I have created a custom post type called Event and I have enabled support for featured images in my functions.php file, but still my custom user role can not see the featured image meta box on the edit page for an event. If a user logges in as administrator the Featured Image meta box is displayed without any errors.

Here is my code to register the CPT:

add_action('init', 'event_post_init');

function event_post_init() {
  // array of all capabilities for our CPT
  $capabilities = array(
     'publish_posts' => 'publish_events',
     'edit_posts' => 'edit_events',
     'edit_others_posts' => 'edit_others_events',
     'delete_posts' => 'delete_events',
     'delete_published_posts' => 'delete_published_events',
     'delete_others_posts' => 'delete_others_events',
     'read_private_posts' => 'read_private_events',
     'edit_post' => 'edit_event',
     'delete_post' => 'delete_event',
     'read_post' => 'read_event',
   );

   // register the CPT
   register_post_type( 'event',
      array(
         'labels' => array(
            'name' => __('Event')
         ),
         'public' => true,
         'has_archive' => true,
         'show_ui' => true,
         'menu_position' => 8,
         'capability_type' => array('event', 'events'),
         'capabilities' => $capabilities,
         'supports' => array('title', 'thumbnail', 'page-attributes'),
         'map_meta_cap' => true,
         'hierarchical' => true,
      )
   );
}

and here is how I setup my custom user role:

function create_event_admin_role(){
   add_role('event_admin', 'Event Administrator', array(
     'publish_events' => false,
     'edit_events' => true,
     'edit_others_events' => false,
     'delete_events' => false,
     'delete_others_events' => false,
     'read_private_events' => true,
     'edit_published_events' => true,
     'read' => true,
     'assign_terms' => true,
     'edit_terms' => true,
     'manage_terms' => true,
     'read_private_pages' => true
   )
  );
}

The strange thing is that if I look at the markup for the edit page I can see the #postimagediv element but for some reason it is hidden. Here is the markup in the page:

<div class="postbox  hide-if-js" id="postimagediv" style="">
   <div title="Click to toggle" class="handlediv"><br></div>
   <h3 class="hndle ui-sortable-handle"><span>Featured Image</span></h3>
   <div class="inside">
     <p class="hide-if-no-js">
       <a class="thickbox" id="set-post-thumbnail" href="http://example.com/wp-admin/media-upload.php?post_id=662&amp;type=image&amp;TB_iframe=1&amp;width=753&amp;height=294" title="Set featured image">Set featured image</a>
      </p>
    </div>
</div>

and the css that actually hides the meta box:

#postimagediv {
   display: hidden !important;
}

Notice that I have enabled Featured Image under Screen Options.

Perhaps I also should point out that I have tried giving the above role the upload_files privilege using the following code:

function extend_event_admin_role() {
  $role = get_role('event_admin');

  $role->add_cap('upload_files');
}

Another thing to point out is that if I do add the upload_files permission, then Featured Image is visible under Screen Options and also other meta boxes which has support for media has a Add Media button, which if I set upload_files to false disappears.

enter image description here

enter image description here

If I change the code in wp-admin/edit-form-advanced.php which add the featured image metabox then I can tell that it’s really calling add_metabox():

if ( $thumbnail_support && current_user_can( 'upload_files' ) ):
  add_meta_box('postimagediv', __('Featured Image'), 'post_thumbnail_meta_box', null, 'side', 'low');
  print 'Support for Featured Image';
  exit;
endif;

3 s
3

Users that are only assigned to your event_admin role, don’t have the upload_files capability, needed to display the featured image meta box.

Here’s the relevant code from the core:

if ( $thumbnail_support && current_user_can( 'upload_files' ) )    // <-- Notice this check
    add_meta_box( 
        'postimagediv', 
        esc_html( $post_type_object->labels->featured_image ),             
        'post_thumbnail_meta_box', 
        null, 
       'side', 
       'low'
    );

Note that if you later try to add the option:

'upload_files' => true

to your add_role() setup, it might not update, since it’s cached in the wp_user_roles option.

You therefore need to update the database to adjust it:

  • use remove_role() and then add_role() again,
  • use the add_cap() method of the \WP_Role class.

It might also be possible to use filters like user_has_cap to adjust this dynamically.

Leave a Reply

Your email address will not be published. Required fields are marked *