我尝试创建一个具有自定义功能的用户角色。目前为止,这个方案是有效的。但如果我想使用函数current_user_can()
来检查某个特定功能的用户权限,它将返回false。但在新创建的角色的功能数组中,特定的功能被设置为true。
因此,为了让我的话更具编码背景:
角色对象
WP_Role {
["name"]=> "organizer"
["capabilities"]=> {
["edit_posts"] => bool(false)
["delete_posts"] => bool(false)
["publish_posts"] => bool(false)
["upload_files"] => bool(true)
["edit_event"] => bool(true)
["read_event"] => bool(true)
["delete_event"] => bool(true)
["edit_events"] => bool(true)
["edit_others_events"] => bool(false)
["publish_events"] => bool(false)
["read_private_events"] => bool(true)
["read"] => bool(true)
["delete_events"] => bool(true)
["delete_private_events"] => bool(false)
["delete_published_events"] => bool(true)
["delete_others_events"] => bool(false)
["edit_private_events"] => bool(false)
["edit_published_events"] => bool(true)
["manage_event_terms"] => bool(true)
["edit_event_terms"] => bool(true)
["delete_event_terms"] => bool(true)
["assign_event_terms"] => bool(true)
}
我的添加角色功能
add_role( 'organizer', __( 'Organizer', 'eventtool' ), array(
// General
'edit_posts' => false,
'delete_posts' => false,
'publish_posts' => false,
'upload_files' => true,
'edit_event' => true,
'read_event' => true,
'delete_event' => true,
// Primitive capabilities used outside of map_meta_cap():
'edit_events' => true,
'edit_others_events' => false,
'publish_events' => false,
'read_private_events' => true,
// Primitive capabilities used within map_meta_cap():
'read' => true,
'delete_events' => true,
'delete_private_events' => false,
'delete_published_events' => true,
'delete_others_events' => false,
'edit_private_events' => false,
'edit_published_events' => true,
'edit_events' => true,
// Terms
'manage_event_terms' => true,
'edit_event_terms' => true,
'delete_event_terms' => true,
'assign_event_terms' => true
)
);
自定义文章类型参数
register_post_type( 'event', array(
'labels' => $labels,
'description' => __( 'This is where you can add new events to your page.', 'eventtool' ),
'public' => true,
'show_ui' => true,
'capability_type' => 'event',
'map_meta_cap' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'hierarchical' => false,
'rewrite' => _x( 'event', 'slug', 'eventtool' ),
'query_var' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
'show_in_nav_menus' => true
)
);
【更新】
function et_modify_map_meta_cap( $caps, $cap, $user_id, $args ) {
var_dump($cap)
}
add_filter( 'map_meta_cap', 'et_modify_map_meta_cap', 10, 4 );
输出“edit_post”,而不是“edit_event”
有什么建议,为什么会出现这种错误行为?
current_user_can
... 而且过滤器map_meta_cap
和这有什么关系?我的var_dump($cap)
显示了edit_events
。 - brasofilo