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

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 cu

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745198236a4616195.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信