目标:创建一个自定义的帖子类型,并只授予管理员和自定义角色查看/控制它的权限。
问题:对于管理员来说,它运行得很好,但对于我获得的自定义角色:Sorry, you are not allowed to access this page.
一开始,我认为这可能只是访问它的能力问题,但这段代码有点不同:
add_submenu_page( /* STAFF PAGES */
'redacted', //Parent Menu Slug
'Staff Pages', //Page Title text
'Staff Pages', //Menu Title text
'edit_staff', //Capability required for this menu to be displayed by user
'edit.php?post_type=staff' //Link to page
);
自定义角色可以看到指向自定义帖子类型的链接,但无法访问它。此外,运行print_r($wp_roles->get_role( 'supervisor' )->capabilities);
确实表明该角色正确地拥有必要的功能。关于如何解决这个问题,我有一些理论,但到目前为止还没有结果。
我的代码如下:
function initialize_plugin(){
//Non-relevant code redacted
add_action( 'admin_init', array($this, 'admin_init') );
}
function activate(){
$this->custom_post_types();
$this->adjust_user_roles();
//Non-relevant code redacted
}
/* My Custom Post Type */
function custom_post_types(){
register_post_type( 'staff', array(
'labels' => array(
//labels redacted
),
'has_archive' => false,
'hierarchical' => true,
'menu_icon' => 'dashicons-groups',
'capability_type' => array('staff', 'staffs'),
'map_meta_cap' => true,
'public' => true,
'show_in_menu' => false,
'rewrite' => array( 'slug' => 'staff', 'with_front' => false ),
'supports' => array( 'title', 'thumbnail', 'custom-fields', 'revisions'),
'show_in_rest' => true,
'taxonomies' => array( 'member-type' ),
'menu_position' => 2,
));
/* My Custom Role */
function adjust_user_roles(){
$wp_roles = new WP_Roles();
$wp_roles->add_role(
'supervisor', __( 'Supervisor' ),
array(
//General
'moderate_comments' => true,
'upload_files' => true,
//Blog Posts
'read' => true,
'read_post' => true,
'edit_post' => true,
'edit_posts' => true,
'edit_others_posts' => true,
'delete_posts' => false, //Can't delete posts
//Staff (Custom Post Type)
'create_staffs' => true,
'read_staff' => true,
'edit_staff' => true,
'edit_staffs' => true,
'edit_others_staffs' => true,
'edit_published_staffs' => true,
'edit_private_staffs' => true,
'delete_staff' => true,
'delete_others_staffs' => true,
'delete_published_staffs' => true,
'delete_private_staffs' => true,
'publish_staffs' => true,
'read_private_staffs' => true,
)
);
/* Adding to administrator */
function admin_init(){
//Non-relevant code redacted
$this->adjust_user_capabilities("add");
}
function adjust_user_capabilities($action, $roles=array('administrator','editor', 'supervisor')){
$staffCaps = array(
'create_staff',
'read_staff',
'edit_staff',
'edit_staffs',
'edit_others_staffs',
'edit_published_staffs',
'edit_private_staffs',
'delete_staff',
'delete_others_staffs',
'delete_published_staffs',
'delete_private_staffs',
'publish_staffs',
'read_private_staffs',
);
//Cycle through each role
foreach($roles as $roleType) :
$role = get_role( $roleType );
//Add each capability
if($action == "add"){
foreach($staffCaps as $staffCap){
$role->add_cap( $staffCap );
}
}
//Remove each capability
elseif($action == "remove"){
foreach($staffCaps as $staffCap){
$role->remove_cap( $staffCap );
}
}
endforeach;
}
注意:此代码出现在wp-content/plugins/myplugin/myplugin.php
中。此外,为了清晰起见,我对代码中一些不相关的部分进行了编辑,例如添加或删除子菜单,并试图阐述更多的结构。请随时告诉我,如果有什么我错过了或有人有问题。:-D
在结束语中:我可能只是一个大白痴,忽略了一些显而易见的事情,但无论如何,我们非常感谢任何和所有的帮助/建议!如果我自己得到了答案,我会把它添加到这个讨论中,以帮助其他面临类似问题的人和/或我未来的自我lol
您可以使用PublishPress Capabilities插件为不同的角色分配多个功能。你可以从这里下载这个插件https://wordpress.org/plugins/capability-manager-enhanced/
解决方案:经过一番周旋,我意识到我绝对是个白痴,而且想得太多了。虽然我之前读过并尝试过这篇类似文章中的一些内容,但我最终用他们的代码代替了我的代码,并发现它实际上适用于我的用例。在试图理解为什么会这样的过程中,我开始尝试将其转换为我的,并很快找到了问题的根源:
/* My Custom Post Type */
function custom_post_types(){
register_post_type( 'staff', array(
'labels' => array(
//labels redacted
),
'has_archive' => false,
'hierarchical' => true,
'menu_icon' => 'dashicons-groups',
'capability_type' => array('staff', 'staffs'),
'map_meta_cap' => true,
'public' => true,
/*---------> */ 'show_in_menu' => false, /* <---------*/
'rewrite' => array( 'slug' => 'staff', 'with_front' => false ),
'supports' => array( 'title', 'thumbnail', 'custom-fields', 'revisions'),
'show_in_rest' => true,
'taxonomies' => array( 'member-type' ),
'menu_position' => 2,
));
为了获得一个干净的自定义菜单,我将show_in_menu
设置为false,这给我带来了问题。当我将其更改为'show_in_menu' => true
时,我的问题得到了解决。在解决这个问题时,我很想尝试remove_menu_page();
,或者考虑一些更优雅的东西。
无论如何,今天的教训是不要过分关注某个方面。希望这能帮助其他人和快乐的编码!