我尝试使用一些自定义功能创建用户角色。这到目前为止工作。但是,如果我想使用函数current_user_can()
检查一个特定功能的用户权限,则返回false。但是在新创建角色的capabilities数组中,特定功能设置为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
)
);
CUSTOM POST TYPE ARGS
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
)
);
[UPDATE]
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'
任何建议,为什么会出现这种错误的行为?
答案 0 :(得分:1)
今天我解决了它。
有必要将第二个参数的对象id分配给current_user_can()
函数,以获得单个指向功能的正确返回(例如,' edit_post')。否则,如果未设置此参数,则函数将返回false。
如果省略,您可能会收到'未定义的偏移:0'警告(这是因为
current_user_can
函数最终调用map_meta_cap
,当检查元功能时需要一个数组,但只提供一个值)
答案 1 :(得分:0)
首先尝试将这些capabilities
注册到自定义帖子类型event
,然后将capabilities
分配给organiser
像这样:
function create_my_post_types() {
register_post_type(
'movie',
array(
'public' => true,
'capability_type' => 'movie',
'capabilities' => array(
'publish_posts' => 'publish_movies',
'edit_posts' => 'edit_movies',
'edit_others_posts' => 'edit_others_movies',
'delete_posts' => 'delete_movies',
'delete_others_posts' => 'delete_others_movies',
'read_private_posts' => 'read_private_movies',
'edit_post' => 'edit_movie',
'delete_post' => 'delete_movie',
'read_post' => 'read_movie',
),
)
);
}
将代码更改为event
帖子类型。
并通过以下代码检查用户功能:
add_filter( 'map_meta_cap', 'my_map_meta_cap', 10, 4 );
function my_map_meta_cap( $caps, $cap, $user_id, $args ) {
/* If editing, deleting, or reading a movie, get the post and post type object. */
if ( 'edit_movie' == $cap || 'delete_movie' == $cap || 'read_movie' == $cap ) {
$post = get_post( $args[0] );
$post_type = get_post_type_object( $post->post_type );
/* Set an empty array for the caps. */
$caps = array();
}
/* If editing a movie, assign the required capability. */
if ( 'edit_movie' == $cap ) {
if ( $user_id == $post->post_author )
$caps[] = $post_type->cap->edit_posts;
else
$caps[] = $post_type->cap->edit_others_posts;
}
/* If deleting a movie, assign the required capability. */
elseif ( 'delete_movie' == $cap ) {
if ( $user_id == $post->post_author )
$caps[] = $post_type->cap->delete_posts;
else
$caps[] = $post_type->cap->delete_others_posts;
}
/* If reading a private movie, assign the required capability. */
elseif ( 'read_movie' == $cap ) {
if ( 'private' != $post->post_status )
$caps[] = 'read';
elseif ( $user_id == $post->post_author )
$caps[] = 'read';
else
$caps[] = $post_type->cap->read_private_posts;
}
/* Return the capabilities required by the user. */
return $caps;
}
答案 2 :(得分:0)
试试user_can()。您需要将用户ID作为第一个参数传递。你可以通过get_current_user_id()获得它。