1

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.

0

Oh, well, while writing the question I tried to copy the full link that takes the ajax call: example.com/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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.