我正在编写一个插件,它创建了一个新的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,
没有达到我想要的效果。我已经阅读了几篇教程,并从中获得了一些代码,其中“元能力”被映射,但我没有在我的代码中使用它,因为我不明白它是如何工作的,而且似乎也不是必需的。我可能错了。