Reputation: 1629
After updating to WordPress 3.5, I started getting the following error when uploading files using the Add Media button as a non-admin user:
Error: An error occurred in the upload. Please try again later.
The image seems to finish uploading, but right at the end this error message appears.
This doesn't happen for the administrator, only the other roles. I've even tried giving the other role full admin capabilities, but the error still appears.
Is this a bug? Or am I missing something?
Upvotes: 4
Views: 16205
Reputation: 145
Im grateful to "Emilien" who posted a question at Wordpress media upload in front-end blocked by admin redirect over 10 years ago, because it led us to see that it was the admin redirect function in our functions.php that was preventing nonAdmin users from getting to the media library, not necessarily their roll capabilities. Then "manishie" on that page posted the link to this page which actually solved our problem. I'm posting this solution in both places. So, based on the basic solution provided in answer above by "HWD", here is the rewrite of our nonAdminRedirect() function in functions.php. (WordPress 6.4.2 as of this writing.)
function nonAdminRedirect() {
// how to see the $_SERVER[] data when accessing media lib
// rprint2f("_SERVER['PHP_SELF'] = ", $_SERVER['PHP_SELF']);
// rprint2f("_SERVER['REQUEST_URI'] = ", $_SERVER['REQUEST_URI']);
// rprint2f() is our own print_r() print to file function, feel free to request
{
if( $_SERVER['PHP_SELF'] == '/wp-login.php' ){
return true; // allow users to login
} else if (
$_SERVER['PHP_SELF'] == '/wp-admin/admin-ajax.php' || // Media Library call
$_SERVER['PHP_SELF'] == '/wp-admin/async-upload.php' || // "Upload Image" clicked
$_SERVER['PHP_SELF'] == '/wp-admin/post.php' // "Edit Image" clicked
)
{
$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 );
Final note: These are the capabilities we assign in a special roll we created called "gw-contributor" to add/edit/delete a special post type "greatworkers": [edit_greatworkers, publish_greatworkers, delete_greatworkers, read, upload_files, edit_files, edit_posts, delete_posts]. Once inside the media library, "edit_posts" is required for the "Edit Image" link to work and "delete_posts" is required for the "Delete Permanently" link.
Upvotes: 0
Reputation: 11
Please see this link for more details - it helped me https://sebastian.expert/fix-wordpress-an-error-occurred-in-the-upload-please-try-again-later/
Basically what it says is to use Developer tools in Chrome or Firefox to see the response from async_upload.php file after uploading files (when error message appears). It returns error details in JSON format. Having details it will be easier and a lot faster to resolve the problem.
Upvotes: 0
Reputation: 394
I resolve my problem with,
sudo apt-get update
sudo apt-get install php5-gd
this message i get in firebug inspect in moment of upload.
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"
so i get this in firebug in moment of upload.
Upvotes: 0
Reputation: 11
I just had this error after updating PHP to 5.3. The problem on me was short_open_tag.
It was off by default. I enabled it and all is OK now.
Upvotes: 1
Reputation: 1629
After much trial and error, I finally found a solution that worked for me.
First, I found the following role capabilities to be required to upload files for custom user roles:
$capabilites = array(
'read' => true,
'upload_files' => true,
'edit_published_pages' => true,
'edit_others_pages' => true
);
I'm not sure why these are specifically required, but the error kept occurring without them.
Second, I had to update a function I was using to prevent non-admin users from accessing the Dashboard:
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 );
Previously, I was checking for the media-upload.php, but the new media uploader uses async-upload.php.
So, essentially, this allows non-admin users to use the new media uploader from the front-end without allowing them access to the Dashboard.
It also restricts their access to the Media Library, which was also important to me.
Upvotes: 8
Reputation: 2186
This could be caused by a couple of different factors, what this usually suggests is:
File is to large
Refeer to this thread on how to up the maximum allowed filesize.
Not enough diskspace
Check if your servers harddrive is full.
Insufficient write permissions
Make sure that PHP and your webserver has write permissions to the wp-uploads folder.
Upvotes: 1