lundi 5 octobre 2015

Custom user role cannot see or modify featured image

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://ift.tt/1JO1xwh" 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;



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire