以下是我为实现这一目标所做的工作。我将使用“blob”作为示例自定义帖子类型。呼叫时register_post_type
, 包括以下参数:
"capability_type" => "blob",
\'capabilities\' => array(
\'create_posts\' => \'create_blob\',
\'publish_posts\' => \'publish_blob\',
\'edit_posts\' => \'edit_blob\',
\'edit_others_posts\' => \'edit_others_blob\',
\'delete_posts\' => \'delete_blob\',
\'delete_others_posts\' => \'delete_others_blob\',
\'read_private_posts\' => \'read_private_blob\',
\'edit_post\' => \'edit_blob\',
\'delete_post\' => \'delete_blob\',
\'read_post\' => \'read_blob\',
),
"map_meta_cap" => false,
您还需要以下代码来映射元功能。
Source.
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 blob, get the post and post type object. */
if ( \'edit_blob\' == $cap || \'delete_blob\' == $cap || \'read_blob\' == $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 blob, assign the required capability. */
if ( \'edit_blob\' == $cap ) {
if ( $user_id == $post->post_author )
$caps[] = $post_type->cap->edit_posts;
else
$caps[] = $post_type->cap->edit_others_posts;
}
/* If deleting a blob, assign the required capability. */
elseif ( \'delete_blob\' == $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 blob, assign the required capability. */
elseif ( \'read_blob\' == $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;
}
现在你需要一个插件来编辑你定义的新的自定义功能。我用过
Members. 使用该插件向用户角色授予或拒绝新功能。对于Members插件和您允许编辑但阻止创建和删除的特定请求,这涉及打开用户->角色菜单项,编辑角色,单击左侧菜单中的“Blob”以筛选与Blob相关的功能,然后拒绝“create\\u Blob”、“delete\\u blogs”和“delete\\u others\\u Blob”。一旦它们被设置好,并且您对它们感到满意,您就可以禁用插件,这些功能将存储在数据库中。