自定义用户角色无法查看或修改特色图像

时间:2015-10-01 作者:Cyclonecode

我创建了一个名为Event 并且我在我的functions.php 文件,但我的自定义用户角色仍然无法看到事件编辑页面上的特色图像元框。如果用户以管理员身份登录Featured Image 元框显示无任何错误。

以下是我注册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,
      )
   );
}
下面是我如何设置自定义用户角色:

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
   )
  );
}
奇怪的是,如果我查看编辑页面的标记,我可以看到#postimagediv 元素,但由于某些原因它被隐藏。以下是页面中的标记:

<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>
而实际上隐藏元框的css:

#postimagediv {
   display: hidden !important;
}
请注意,我have 已启用Featured Image 在下面Screen Options.

也许我还应该指出,我已经尝试过赋予上述角色upload_files 使用以下代码的权限:

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

  $role->add_cap(\'upload_files\');
}
另一件需要指出的是,如果我do 添加upload_files 许可,那么Featured Image 在下可见Screen Options 还有其他支持媒体的元框Add Media 按钮,如果我设置upload_filesfalse 消失。

enter image description here

enter image description here

如果我在中更改代码wp-admin/edit-form-advanced.php 它添加了特色图像元盒,然后我可以看出它真的在呼叫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 个回复
最合适的回答,由SO网友:Cyclonecode 整理而成

我最终找到了一个激活的插件,名为Revisionary. 负责隐藏postimagediv 元素为act_hide_admin_divs 位于revisionary/admin/admin_rvy.php. 该函数的作用是隐藏包含不支持修订的内容的元数据库。

为了显示我特定角色的特色图像,我使用rvy_hidden_meta_boxes 过滤器:

add_filter(\'rvy_hidden_meta_boxes\', \'revisor_show_featured_image_box\');

function revisor_show_featured_image_box($unrevisable_css_ids) {
   $key = array_search(\'postimagediv\', $unrevisable_css_ids, true);
   unset($unrevisable_css_ids[$key]);

   return $unrevisable_css_ids;
}

SO网友:birgire

仅分配给您的event_admin 角色,没有upload_files 功能,需要显示特征图像元框。

以下是核心的相关代码:

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\'
    );
请注意,如果稍后尝试添加选项:

\'upload_files\' => true
到您的add_role() 安装程序,它可能不会更新,因为它缓存在wp_user_roles 选项

因此,您需要更新数据库以进行调整:

使用remove_role() 然后add_role() 再次使用add_cap() 的方法\\WP_Role 类别user_has_cap 动态调整。

SO网友:Webloper

据我所知,您需要自定义帖子类型Event + featured_image + Custom Role, 我不了解的是capability\\u类型的事件和事件,除非并且直到您要定制很多,否则这种定制的原因是什么?

我在这里所做的是克隆编辑器角色event_amdin 使用它的所有功能,并在问题中使用页面属性时将capability\\u type设置为页面。

测试:创建了两个用户,分别作为参与者和事件管理员,其工作正常。请提供有关您需要的更多信息,以便我更新此答案。谢谢

Edit 1 : 更新代码,选中“编辑1开始/结束部分到”cloneUserRole 作用

快乐的编码!!

function cloneUserRole()
{
    global $wp_roles;

    if (!isset($wp_roles))
        $wp_roles = new WP_Roles();

        $editor      = $wp_roles->get_role(\'editor\');
        // Adding a new role with all editor caps.
        $wp_roles->add_role(\'event_admin\', \'Event Administrator\', $editor->capabilities);

        // edit 1 start : updated to add cap to new user role
        $event_admin      = $wp_roles->get_role(\'event_admin\');

        $event_admin->add_cap( \'read_event\' );
        $event_admin->add_cap( \'delete_event\' );
        $event_admin->add_cap( \'edit_event\' );
        $event_admin->add_cap( \'read_private_events\' );
        $event_admin->add_cap( \'delete_others_events\' ); // don\'t add
        $event_admin->add_cap( \'delete_published_events\' );
        $event_admin->add_cap( \'delete_events\' ); // don\'t add
        $event_admin->add_cap( \'edit_others_events\' ); // don\'t add
        $event_admin->add_cap( \'edit_events\' );
        $event_admin->add_cap( \'publish_events\' ); // don\'t add
        // edit 1 ends : 

        //echo \'<pre>\', print_r( $wp_roles, 1), \'</pre>\';
        //die;
}
add_action(\'init\', \'cloneUserRole\');


function stack_event_init() {
    $labels = array(
        \'name\'               => _x( \'Events\', \'post type general name\', \'stack\' ),
        \'singular_name\'      => _x( \'Event\', \'post type singular name\', \'stack\' ),
        \'menu_name\'          => _x( \'Events\', \'admin menu\', \'stack\' ),
        \'name_admin_bar\'     => _x( \'Event\', \'add new on admin bar\', \'stack\' ),
        \'add_new\'            => _x( \'Add New\', \'event\', \'stack\' ),
        \'add_new_item\'       => __( \'Add New Event\', \'stack\' ),
        \'new_item\'           => __( \'New Event\', \'stack\' ),
        \'edit_item\'          => __( \'Edit Event\', \'stack\' ),
        \'view_item\'          => __( \'View Event\', \'stack\' ),
        \'all_items\'          => __( \'All Events\', \'stack\' ),
        \'search_items\'       => __( \'Search Events\', \'stack\' ),
        \'parent_item_colon\'  => __( \'Parent Events:\', \'stack\' ),
        \'not_found\'          => __( \'No events found.\', \'stack\' ),
        \'not_found_in_trash\' => __( \'No events found in Trash.\', \'stack\' )
    );

    $args = array(
        \'labels\'             => $labels,
        \'description\'        => __( \'Description.\', \'stack\' ),
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_nav_menus\'  => true,
        \'show_in_menu\'       => true,
        \'show_in_admin_bar\'  => true,
        \'query_var\'          => true,
        \'rewrite\'            => array( \'slug\' => \'event\' ),
        \'capability_type\'    => \'page\',
        \'has_archive\'        => true,
        \'hierarchical\'       => true,
        \'menu_position\'      => null,
        \'supports\'           => array( \'title\', \'thumbnail\', \'page-attributes\' )
    );

    register_post_type( \'event\', $args );
}
add_action(\'init\', \'stack_event_init\');

function my_rewrite_flush() {
    stack_event_init();
    flush_rewrite_rules();
}
add_action( \'after_switch_theme\', \'my_rewrite_flush\' );
  1. Roles and Capabilities
  2. Register post type
  3. Flush Rewrite Rules

相关推荐