wordpress register_post_type capability gutenberg
register_post_type
is used in WordPress to create a new custom post type. You can define various parameters and settings for the custom post type, including specifying the capabilities required to manage these posts. Here's how you can use register_post_type
to create a custom post type with capabilities in Gutenberg:
Define the Labels: You start by setting up labels for your custom post type, such as its name, singular and plural forms, and other descriptors.
Set Capability Type: Specify the capability type required to manage these posts. For instance, if you want only administrators to manage these posts, you'd set the capability type as
'capability_type' => 'post', 'capabilities' => array('create_posts' => 'do_not_allow')
.Assign Capabilities: Use the
capabilities
parameter withinregister_post_type
to assign specific capabilities to different user roles. For example:
php
'capability_type' => 'custom_post_type',
'capabilities' => array(
'edit_post' => 'edit_custom_post',
'read_post' => 'read_custom_post',
'delete_post' => 'delete_custom_post',
'edit_posts' => 'edit_custom_posts',
'edit_others_posts' => 'edit_others_custom_posts',
// Add more capabilities as needed...
),
'map_meta_cap' => true,
This code assigns capabilities like editing, reading, and deleting posts for the custom post type to different user roles.
- Capability Mapping: Using
'map_meta_cap' => true
allows WordPress to map the meta capabilities likeedit_custom_post
,read_custom_post
, etc., to their respective primitive capabilities (e.g.,edit_posts
,read
,delete_posts
).
By setting these parameters within register_post_type
, you define the capabilities required to manage the custom post type in Gutenberg and other areas of WordPress.