最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

media - 'An error occurred in the upload. Please try again later.' for users with different roles

programmeradmin0浏览0评论

The problem seems to be quite common, yet most of the solutions on the net are nothing to do with my issue.

So I have custom post type where on the front end users can add a post and upload an image in that post. I'm using the following script to allow access for the subscribers to upload media:

function add_media_cap()
{
    global $current_user;
    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);

    if ($user_role == 'subscriber') {
        $subscriber = get_role('subscriber');
        $subscriber->add_cap('upload_files');
        $subscriber->add_cap('edit_files');
    }
}
add_action('init', 'add_media_cap');

Also have this one too, to restrict them from seeing someone else media:

function show_current_user_attachments($query)
{
    $user_id = get_current_user_id();
    if ($user_id) {
        $query['author'] = $user_id;
    }
    return $query;
}
add_filter('ajax_query_attachments_args', 'show_current_user_attachments',10, 1);

Thing is that with admin account I can upload image with no problem. But subscribers can't, and they get the above mentioned error. I have checked the response from 'async-upload.php' and it is empty. Empty is also the console log in the developers tools, empty is the error log. All file permissions are as they should be.

Any help would be welcomed.

The problem seems to be quite common, yet most of the solutions on the net are nothing to do with my issue.

So I have custom post type where on the front end users can add a post and upload an image in that post. I'm using the following script to allow access for the subscribers to upload media:

function add_media_cap()
{
    global $current_user;
    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);

    if ($user_role == 'subscriber') {
        $subscriber = get_role('subscriber');
        $subscriber->add_cap('upload_files');
        $subscriber->add_cap('edit_files');
    }
}
add_action('init', 'add_media_cap');

Also have this one too, to restrict them from seeing someone else media:

function show_current_user_attachments($query)
{
    $user_id = get_current_user_id();
    if ($user_id) {
        $query['author'] = $user_id;
    }
    return $query;
}
add_filter('ajax_query_attachments_args', 'show_current_user_attachments',10, 1);

Thing is that with admin account I can upload image with no problem. But subscribers can't, and they get the above mentioned error. I have checked the response from 'async-upload.php' and it is empty. Empty is also the console log in the developers tools, empty is the error log. All file permissions are as they should be.

Any help would be welcomed.

Share Improve this question asked Aug 30, 2019 at 20:49 AleAle 1807 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Oh, well, while writing the question I tried to copy the full link that takes the ajax call: example/wp-admin/async-upload.php and noticed that for the subscibers it was taking the user to their account page while the admins where getting The link you followed has expired. Notice on the screen. And then I got the 'Aha' moment - I was restricting no-admins from wp-admin and redirected them to their account page. After removing this redirect it all worked.

发布评论

评论列表(0)

  1. 暂无评论