I'm trying to add a custom post types with capabilities, so a custom user role can do certain things with them. Eventually the custom role is a Client, and the post a Website, and I want clients to be able to log in and view their websites. However, the post type is not showing up as soon as I add capabilities => 'website'
. Why is is not showing up? I was following this tutorial and here it seems to work at 3:54 (with vehicles as a custom post type).
<?php
// Creating the website post type
function add_website_post_type() {
$website_post_type_args = array(
'labels' => $website_post_type_labels,
'public' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-feedback',
'hierarchical' => false,
'supports' => array( 'title' ),
'has_archive' => true,
'capability_type' => 'website'
);
// Define all the labels
$website_post_type_labels = array(
'name' => __( 'Websites' ),
'singular_name' => __( 'Website' ),
'add_new_item' => __( 'Add New Website' ),
'edit_item' => __( 'Edit Website' ),
'new_item' => __( 'New Website' ),
'view_item' => __( 'View Website' ),
'view_items' => __( 'View Websites' ),
'not_found' => __( 'No Websites Found' ),
'not_found_in_trash' => __( 'No Deleted Websites Found' ),
'all_items' => __( 'All Websites' ),
'archives' => __( 'Website Archives' ),
'insert_into_item' => __( 'Insert Into Website' ),
'uploaded_to_this_item' => __( 'Uploaded To This Website' )
);
register_post_type( 'website', $website_post_type_args );
}
add_action( 'init', 'add_website_post_type', 10 );
// Create the client user role
function add_client_user_role() {
$capabilities = array(
'publish_website' => true,
'edit_website' => true,
'edit_others_website' => true,
'delete_website' => true,
'delete_others_website' => true,
'read_website' => true,
'read_private_website' => true,
'read'
);
add_role( 'client', 'Client', $capabilities );
}
add_action( 'init', 'add_client_user_role', 10 );
I'm trying to add a custom post types with capabilities, so a custom user role can do certain things with them. Eventually the custom role is a Client, and the post a Website, and I want clients to be able to log in and view their websites. However, the post type is not showing up as soon as I add capabilities => 'website'
. Why is is not showing up? I was following this tutorial and here it seems to work at 3:54 (with vehicles as a custom post type).
<?php
// Creating the website post type
function add_website_post_type() {
$website_post_type_args = array(
'labels' => $website_post_type_labels,
'public' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-feedback',
'hierarchical' => false,
'supports' => array( 'title' ),
'has_archive' => true,
'capability_type' => 'website'
);
// Define all the labels
$website_post_type_labels = array(
'name' => __( 'Websites' ),
'singular_name' => __( 'Website' ),
'add_new_item' => __( 'Add New Website' ),
'edit_item' => __( 'Edit Website' ),
'new_item' => __( 'New Website' ),
'view_item' => __( 'View Website' ),
'view_items' => __( 'View Websites' ),
'not_found' => __( 'No Websites Found' ),
'not_found_in_trash' => __( 'No Deleted Websites Found' ),
'all_items' => __( 'All Websites' ),
'archives' => __( 'Website Archives' ),
'insert_into_item' => __( 'Insert Into Website' ),
'uploaded_to_this_item' => __( 'Uploaded To This Website' )
);
register_post_type( 'website', $website_post_type_args );
}
add_action( 'init', 'add_website_post_type', 10 );
// Create the client user role
function add_client_user_role() {
$capabilities = array(
'publish_website' => true,
'edit_website' => true,
'edit_others_website' => true,
'delete_website' => true,
'delete_others_website' => true,
'read_website' => true,
'read_private_website' => true,
'read'
);
add_role( 'client', 'Client', $capabilities );
}
add_action( 'init', 'add_client_user_role', 10 );
Share
Improve this question
asked Oct 21, 2020 at 17:58
TomTom
1686 bronze badges
0
1 Answer
Reset to default 2You have to register custom capabilities and then apply inside the register post type function
function register_website_caps() {
$admins = get_role( 'administrator' );
$admins->add_cap( 'edit_website' );
$admins->add_cap( 'read_website' );
$admins->add_cap( 'delete_website' );
$admins->add_cap( 'edit_websites' );
$admins->add_cap( 'edit_others_websites' );
$admins->add_cap( 'publish_websites' );
$admins->add_cap( 'read_private_websites' );
}
add_action( 'admin_init', 'register_website_caps');
// Creating the website post type
function add_website_post_type() {
// Custom capabilities mapping
$capabilities = array(
'edit_post' => 'edit_website',
'read_post' => 'read_website',
'delete_post' => 'delete_website',
'edit_posts' => 'edit_websites',
'edit_others_posts' => 'edit_others_websites',
'publish_posts' => 'publish_websites',
'read_private_posts' => 'read_private_websites',
);
// Define all the labels
$website_post_type_labels = array(
'name' => __( 'Websites' ),
'singular_name' => __( 'Website' ),
'add_new_item' => __( 'Add New Website' ),
'edit_item' => __( 'Edit Website' ),
'new_item' => __( 'New Website' ),
'view_item' => __( 'View Website' ),
'view_items' => __( 'View Websites' ),
'not_found' => __( 'No Websites Found' ),
'not_found_in_trash' => __( 'No Deleted Websites Found' ),
'all_items' => __( 'All Websites' ),
'archives' => __( 'Website Archives' ),
'insert_into_item' => __( 'Insert Into Website' ),
'uploaded_to_this_item' => __( 'Uploaded To This Website' )
);
$website_post_type_args = array(
'labels' => $website_post_type_labels,
'public' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-feedback',
'hierarchical' => false,
'supports' => array( 'title' ),
'has_archive' => true,
'capabilities' => $capabilities,
'map_meta_cap' => true //Whether to use the internal default meta capability handling.
);
register_post_type( 'website', $website_post_type_args );
}
add_action( 'init', 'add_website_post_type', 10 );