【问题标题】:Wordpress Custom role with custom cababilities can't access the dashboard具有自定义功能的 Wordpress 自定义角色无法访问仪表板
【发布时间】:2012-01-03 00:34:11
【问题描述】:

我正在编写一个插件,它创建一个新的 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...
}

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

【问题讨论】:

    标签: wordpress


    【解决方案1】:

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

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

    ...它成功了!我很想知道为什么 add_cap 方法有效,而在功能数组中声明它却无效。

    【讨论】:

    • 也为我工作。在功能定义中添加“读取”应该是可行的,但只有以您展示的方式明确似乎才能完成这项工作。
    • 你是救生员。三年多后这仍然是一个问题,但上述解决方案非常有效。
    【解决方案2】:

    您将获得 NULL,因为您已经拥有角色 dictionary_entry。 您可以创建另一个具有不同角色名称的角色,此外 - 您可以删除或更改存在的上限。

    【讨论】:

      猜你喜欢
      • 2019-11-20
      • 2018-04-29
      • 2018-03-10
      • 1970-01-01
      • 2011-07-29
      • 2019-04-16
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多