1

I have a page template, where users can upload images. I use this piece of code for opening modal for media (js; only part of a code):

image_frame = wp.media({
  title: 'Select Media',
  multiple : 'add',
  button: {
    text: 'Use this media'
  },
});

I have the following custom role:

add_role( 'customer',  __( 'Customer' ),
        array(
          'read'         => true,
          'edit_files'   => true,
          'upload_files'   => true,
          'read_post'   => true,
          'edit_post'   => true,
          'edit_others_post'   => true,
        )
      );

The custom post type also has

'upload_files' => true,

With this, users with the role of 'customer' can upload new files on the admin area. When they open the page template, a modal for media appears, and they can choose already uploaded image, but if Customers try to upload new image from the page template, they get the following message:

Sorry, you are not allowed to attach files to this post.

Any help or suggestions will be appreciated.

Thanks.

2
0

I have added this code to enable everyone to add files

function allow_own_attachments( $user_caps, $req_caps, $args, $UserObj ) {
if ( empty($args[2]) ) {
   return $user_caps;  // nothing to check
}
$post = get_post( $args[2] );  // post_id was passed here

if (is_object($post)){  //check if $post is an object. If it is't checked the code throws this Notice: Trying to get property 'post_author' of non-object 
    if ( $post->post_author == $UserObj->ID ) {  // this is my post
        foreach ( (array) $req_caps as $cap ) {
            if ( empty( $user_caps[ $cap ] ) )
                $user_caps[ $cap ] = true;
        }
    }
}
$user_caps['edit_post'] = true; // tested by wp_ajax_upload_attachment()
return $user_caps;
}

add_filter( 'user_has_cap', 'allow_own_attachments', 10, 4 );

"...any custom filter that is determined to give permission must loop through the array and set everything true."

I've used the code from another question. Original answer can be found here:
Add Media Upload Capabilities Needed for Custom Role for non-Posts

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.