I have created a custom post type called Course Documents using WordPress Custom Post Type UI plugin.
Also, I have created a new user role called Teacher.
add_role('rpt_teacher',
'Teacher',
array(
'read' => true,
'edit_posts' => false,
'delete_posts' => false,
'publish_posts' => false,
'upload_files' => true,
)
);
And now I want to enable the custom post type in teacher dashboard nav menu.
I have used below code in my functions.php
but nothing happens. How can I resolve my issue?
add_action('admin_init','rpt_add_role_caps',999);
/**
add teachers capability
*/
function rpt_add_role_caps() {
// Add the roles you'd like to administer the custom post types
$roles = array('rpt_teacher','editor','administrator');
// Loop through each role and assign capabilities
foreach($roles as $the_role) {
$role = get_role($the_role);
$role->add_cap( 'read' );
$role->add_cap( 'read_course_document');
$role->add_cap( 'edit_course_document' );
$role->add_cap( 'edit_course_documents' );
$role->add_cap( 'edit_published_course_documents' );
$role->add_cap( 'publish_course_documents' );
$role->add_cap( 'delete_published_course_documents' );
}
}
4 Answers
I don’t think the plugin adds the capabilities you are using in add_cap
while registering the post type.
You can modify the registered post type by adding code to themes functions.php
file. You can do something like this.
/**
* Overwrite args of custom post type registered by plugin
*/
add_filter( 'register_post_type_args', 'change_capabilities_of_course_document' , 10, 2 );
function change_capabilities_of_course_document( $args, $post_type ){
// Do not filter any other post type
if ( 'course_document' !== $post_type ) {
// Give other post_types their original arguments
return $args;
}
// Change the capabilities of the "course_document" post_type
$args['capabilities'] = array(
'edit_post' => 'edit_course_document',
'edit_posts' => 'edit_course_documents',
'edit_others_posts' => 'edit_other_course_documents',
'publish_posts' => 'publish_course_documents',
'read_post' => 'read_course_document',
'read_private_posts' => 'read_private_course_documents',
'delete_post' => 'delete_course_document'
);
// Give the course_document post type it's arguments
return $args;
}
Then you can do
/**
add teachers capability
*/
add_action('admin_init','rpt_add_role_caps',999);
function rpt_add_role_caps() {
$role = get_role('teacher');
$role->add_cap( 'read_course_document');
$role->add_cap( 'edit_course_document' );
$role->add_cap( 'edit_course_documents' );
$role->add_cap( 'edit_other_course_documents' );
$role->add_cap( 'edit_published_course_documents' );
$role->add_cap( 'publish_course_documents' );
$role->add_cap( 'read_private_course_documents' );
$role->add_cap( 'delete_course_document' );
}
You don’t need to add capability to administrator and editor because the capability_type
is post
by default while registering Custom Post Type via this plugin. You can change it, if you prefer to have custom capability_type
based on other post type.
Note: Make sure Show in Menu
is set to true
. It is true
by default in this plugin.