更新到 WordPress 3.5 后,以非管理员用户身份使用“添加媒体”按钮上传文件时开始出现以下错误:
错误:上传时发生错误。请稍后重试。
图像似乎已完成上传,但最后出现此错误消息。
这种情况不会发生在管理员身上,只会发生在其他角色身上。我什至尝试为其他角色提供完整的管理权限,但错误仍然出现。
这是一个错误吗?还是我错过了什么?
经过多次尝试和错误,我终于找到了适合我的解决方案。
首先,我发现上传自定义用户角色的文件需要以下角色功能:
$capabilites = array(
'read' => true,
'upload_files' => true,
'edit_published_pages' => true,
'edit_others_pages' => true
);
我不确定为什么特别需要这些,但如果没有它们,错误就会不断发生。
其次,我必须更新我用来防止非管理员用户访问仪表板的功能:
function redirect_nonadmin_fromdash(){
if($_SERVER['PHP_SELF'] == '/wp-admin/async-upload.php'){
/* allow users to upload files */
return true;
} else if(get_user_role() != 'administrator'){
/* custom function get_user_role() checks user role,
requires administrator, else redirects */
wp_safe_redirect(home_url());
exit;
}
}
add_action( 'login_form_login', 'redirect_nonadmin_fromdash' );
add_action( 'admin_init', 'redirect_nonadmin_fromdash', 1 );
之前,我正在检查 media-upload.php,但新的媒体上传器使用 async-upload.php。
因此,本质上,这允许非管理员用户从前端使用新媒体上传器,而不允许他们访问仪表板。
它还限制了他们对媒体库的访问,这对我来说也很重要。
这可能是由几个不同的因素引起的,这通常表明:
文件太大
请参阅this线程,了解如何增加允许的最大文件大小。
磁盘空间不足
检查您的服务器硬盘是否已满。
写入权限不足
确保 PHP 和您的网络服务器对 wp-uploads 文件夹具有写入权限。
我在将 PHP 更新到 5.3 后才遇到此错误。我的问题是short_open_tag。
默认是关闭的。我启用了它,现在一切正常。
我解决了我的问题,
sudo apt-get update
sudo apt-get install php5-gd
我在上传时在 Firebug 检查中收到此消息。
GD Library Error: imagecreatetruecolor does not exist - please contact your webhost and ask them to install the GD libraryGD Library Error: imagecreatetruecolor does not exist - please contact your webhost and ask them to install the GD library{"success":true,"data":{"id":17,"title":"yoshi","filename":"yoshi1.jpg"
所以我在上传时在萤火虫中得到了这个。
请参阅此链接了解更多详细信息 - 它帮助了我https://sebastian.expert/fix-wordpress-an-error-occurred-in-the-upload-please-try-again-later/
基本上它说的是在上传文件后(出现错误消息时)使用 Chrome 或 Firefox 中的开发者工具查看 async_upload.php 文件的响应。它以 JSON 格式返回错误详细信息。有了详细信息,解决问题会更容易、更快。
我很感谢“Emilien”,他在 10 多年前在WordPress 媒体上传在前端被管理重定向阻止 发布了一个问题,因为它让我们看到是我们的functions.php 中的管理重定向功能阻止非管理员用户访问媒体库,不一定是他们的滚动功能。然后该页面上的“manishie”发布了该页面的链接,这实际上解决了我们的问题。我在这两个地方都发布了这个解决方案。因此,基于“HWD”在上面的答案中提供的基本解决方案,这里是对functions.php中nonAdminRedirect()函数的重写。 (撰写本文时 WordPress 6.4.2。)
function nonAdminRedirect() {
if( $_SERVER['PHP_SELF'] == '/wp-login.php' ){
return true; // allow users to login
} else if (
$_SERVER['PHP_SELF'] == '/wp-admin/admin-ajax.php' ||
$_SERVER['PHP_SELF'] == '/wp-admin/post.php') {
// 1st condition allows user to see photos or upload files in media library
// 2nd condition allows user to click "Edit Image" option when photo is selected;
$roles_array = ['administrator', 'gw-contributor'];
// Check if the user is one of the specified roles
$user_has_allowed_role = false;
foreach ($roles_array as $allowed_role) {
if (in_array($allowed_role, $current_user->roles)) {
$user_has_allowed_role = true;
break;
}
}
if ($user_has_allowed_role) {
return true;
} else {
wp_safe_redirect(site_url());
exit;
}
} else {
// otherwise this is not-a-login && not-an-ajax-call
if(current_user_can('administrator') != 'administrator'){
// Non-admin access to dashboard or other admin
wp_safe_redirect(site_url());
exit;
}
}
}
add_action( 'login_form_login', 'nonAdminRedirect' );
add_action( 'admin_init', 'nonAdminRedirect', 1 );
最后说明:这些是我们在创建的名为“gw-contributor”的特殊卷中分配的功能,用于添加/编辑/删除特殊帖子类型“greatworkers”:edit_greatworkers、publish_greatworkers、delete_greatworkers、read、upload_files、edit_files、edit_posts。进入媒体库后,“编辑图像”链接需要“edit_posts”才能工作。