具有自定义功能的Wordpress自定义角色无法访问仪表板

eme*_*his 4 wordpress wordpress-plugin

我正在编写一个插件,它创建一个新的post_type和一个相应的角色,只能添加/编辑/等新的post_type.一切都按预期工作,但分配了我的自定义角色的用户无法查看仪表板.用户登录后,会立即收到以下错误:

"您没有足够的权限来访问此页面."

但是,登录成功,当用户浏览网站时,"编辑"链接会显示在自定义帖子上.该用户甚至可以编辑/通过工具栏添加新的自定义职位,但只要他/她点击"仪表盘"或其他任何环节没有直接联系到自定义post_type他们得到上面的错误消息.

我认为这意味着我的自定义功能正在运行......但太好了(因为太严格了).我还想允许基本的"订阅者"权限以及我为自定义post_type定义的功能.我已阅读并重新阅读能力与角色的抄本,看来,我应该能够做什么,我希望通过添加'read' => true到我的能力阵列,但它似乎并不奏效.这是我的代码:

add_action('init', 'setup_dictionary_post');

//Register custom post type 
function setup_dictionary_post() {

$capabilities = array(
    'publish_posts' => 'publish_dictionary_entry',
    'edit_posts' => 'edit_dictionary_entry',
    'edit_others_posts' => 'edit_others_dictionary_entry',
    'delete_posts' => 'delete_dictionary_entry',
    'delete_others_posts' => 'delete_others_dictionary_entry',
    'read_private_posts' => 'read_private_dictionary_entry',
    'edit_post' => 'edit_dictionary_entry',
    'delete_post' => 'delete_dictionary_entry',
    'read_post' => 'read_dictionary_entry'
);

$labels = array(
    'name' => 'Dictionary Entries',
      'singular_name' => 'Dictionary Entry',
      'menu_name' => 'Dictionary Entries',
      'add_new' => 'Add New',
      'add_new_item' => 'Add New Dictionary Entry',
      'edit' => 'Edit entry',
      'edit_item' => 'Edit Dictionary Entry',
      'new_item' => 'New Dictionary Entry',
      'view' => 'View Dictionary Entry',
      'view_item' => 'View Dictionary Entry',
      'search_items' => 'Search Dictionary Entries',
      'not_found' => 'No Dictionary Entries Found',
      'not_found_in_trash' => 'No Dictionary Entries Found in Trash',
      'parent' => 'Parent Dictionary Entry',);

    register_post_type('dictionary_entry', array(
        'label' => 'Dictionary Entries',
        'description' => '',
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'capability_type' => 'dictionary_entry', 
        'capabilities'=>$capabilities, 
        'hierarchical' => false,
        'rewrite' => array('slug' => ''),
        'query_var' => true,
        'supports' =>     array('title','comments','revisions','thumbnail','author','page-attributes',),
        'labels' => $labels,
        )
    );

    flush_rewrite_rules(false);

    /********************** CUSTOM ROLE *****************************/
    add_role('dictionary_entry_author', 'Dictionary Helper', array(
        'publish_dictionary_entry' => true,
        'edit_dictionary_entry' => true,
        'edit_others_dictionary_entry' => true,
        'delete_dictionary_entry' => true,
        'delete_others_dictionary_entry' => true,
        'read_private_dictionary_entry' => true,
        'edit_dictionary_entry' => true,
        'delete_dictionary_entry' => true,
        'read_dictionary_entry' => true,
        // more standard capabilities here
'read' => true,
    ));

//I do some other stuff later in this function that isn't important.
//For example, I define a custom taxonomy for the new post_type...
}
Run Code Online (Sandbox Code Playgroud)

我不确定为什么'read'=>true,没有完成我想要的东西.我读过一些教程,并从其中"元能力"被映射得到了一些代码,但我不会在我的代码使用它,因为我不明白它是如何工作的,它不会似乎是必要的.我错了.

eme*_*his 9

我不知道为什么这有效,但我在add_role()之后立即添加了这个:

    $role =& get_role('dictionary_entry_author'); 
    $role->add_cap('read');
Run Code Online (Sandbox Code Playgroud)

......它工作了!我很想知道为什么这个add_cap方法有效,而在能力数组中声明它却没有.