WordPress自定义角色具有自定义权限,无法访问仪表板。

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

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

然而,登录是成功的,并且当用户浏览网站时,"编辑"链接出现在自定义文章上。这个用户甚至可以通过工具栏编辑/添加新的自定义文章,但是一旦他/她点击"仪表板"或任何其他与自定义post_type直接不相关的链接,他们就会收到上述错误消息。

我认为这意味着我的自定义功能正在起作用...但是太好了(也就是太严格了)。我想同时允许基本的"订阅者"权限和我为自定义post_type定义的功能。我已经反复阅读了有关功能和角色的codex,似乎通过将'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...
}

我不确定为什么 'read'=>true, 没有达到我想要的效果。我已经阅读了几篇教程,并从中获得了一些代码,其中“元能力”被映射,但我没有在我的代码中使用它,因为我不明白它是如何工作的,而且似乎也不是必需的。我可能错了。

2个回答

9

我不知道为什么这个可以起作用,但是在add_role()之后立即添加了以下内容:

    $role =& get_role('dictionary_entry_author'); 
    $role->add_cap('read');

……然后它奏效了!我很想知道为什么add_cap方法奏效,而在能力数组中声明它则不行。


1
对我也起作用了。在能力定义中添加“读取”应该是有效的,但只有像你演示的那样明确地表达才能完成工作。 - irms
你真是个救星。这个问题三年后仍然存在,但上述解决方案非常出色。 - Shellbot

0

你会得到NULL,因为你已经拥有角色dictionary_entry。 你可以创建另一个具有不同角色名称的角色,此外- 你可以删除或更改现有角色的能力。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接