admin管理员组文章数量:1323723
I'm trying to configure capabilities for a custom post type. I want to enable editing, but disable creating and deleting. create_posts
cannot be set to false unless delete_posts
is also false, but it seems that this combination disables the editing screen.
Thanks!
I'm trying to configure capabilities for a custom post type. I want to enable editing, but disable creating and deleting. create_posts
cannot be set to false unless delete_posts
is also false, but it seems that this combination disables the editing screen.
Thanks!
Share Improve this question asked Apr 22, 2015 at 18:05 jdpjdp 1561 silver badge13 bronze badges 3 |2 Answers
Reset to default 1There are a lot of capabilities, you can set for your post type.
foreach ( self::$todo_roles as $role ) {
$wp_roles->add_cap( $role, 'edit_' . self::$post_type_1 );
$wp_roles->add_cap( $role, 'read_' . self::$post_type_1 );
$wp_roles->add_cap( $role, 'delete_' . self::$post_type_1 );
$wp_roles->add_cap( $role, 'edit_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'edit_others_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'publish_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'read_private_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'delete_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'delete_private_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'delete_published_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'delete_others_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'edit_private_' . self::$post_type_1 . 's' );
$wp_roles->add_cap( $role, 'edit_published_' . self::$post_type_1 . 's' );
}
With something like the above you can add capabilities the roles.
As hint to the source. The self::$post_type_1
is a variable in a class for the custom post type. The foreach
loop is to add this capabilities often to different roles.
Here is what I've done to achieve this. I'll use "blob" as my example custom post type. When calling register_post_type
, include the following args:
"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,
You will also need the following code to map the meta capabilities. 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;
}
Now you will need a plugin in order to edit the new custom capabilities you've defined. I used Members. Use the plugin to grant or deny the new capabilities to user roles. For Members plugin and your specific request of allowing editing but preventing creating and deleting, this involves opening the Users->Roles menu item, editing a role, click on "Blob" in the left side menu to filter for capabilities related to Blob, and then denying "create_blobs", "delete_blogs", and "delete_others_blobs". Once they are set and you're happy with them you can disable the plugin, the capabilities will be stored in the database.
本文标签: Custom Post Type CapabilitiesEnable EditDisable Create and Delete
版权声明:本文标题:Custom Post Type Capabilities -- Enable Edit, Disable Create and Delete 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742122812a2421801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
create_posts
- might this just be the source of your problem? See Codex: Roles and Capabilities for more information. – Nicolai Grossherr Commented Apr 22, 2015 at 20:06