5

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 4

15

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.

1
  • 1
    Perfect, thanks! One gotcha I encountered: "If you are defining a custom role, and adding capabilities to the role using add_role(), be aware that modifying the capabilities array and re-executing add_role() will not necessarily update the role with the new capabilities list. The add_role() function short-circuits if the role already exists in the database. The workaround in this case is to precede your add_role() call with a remove_role() call that targets the role you are adding." codex.wordpress.org/Function_Reference/…
    – 00-BBB
    Commented May 23, 2019 at 16:02
2

The way I see it, you are on the right track. You created a user and assign new capabilities to it. You just missed specifying that the new capabilities will be used to edit your custom post type.

I have never used a plugin to create a custom post type so I am not sure how to do that using a plugin. But if you are willing to create your CPT via code you can use the following:

add_action( 'init', 'psp_register_cpt_projects');
function psp_register_cpt_projects() {
     $args = array(
 'label'               => __( 'course_documents', 'course_documents' ),
 'description'         => __( 'Course document', 'course_document' ),
 'supports'            => array( 'title', 'comments', 'revisions', ),
 'hierarchical'        => false,
 'public'              => true,
 'show_ui'             => true,
 'capability_type'     => array('course_document','course_documents'),
 'map_meta_cap'        => true,
 );
 register_post_type( 'course_documents', $args );
}

Note: This is just a basic code for creating a CPT. Please read this doc in the codex to understand it thoroughly.

Let me know if this helps you.

1
  • Yes. When I create a custom post type I use your suggestion. But there are lot of custom post type in this project that created by another developer and I don't want to rewrite all of them as above. Commented Jan 4, 2018 at 10:03
1

I just allowed all users to show in the 'Author' selection:

add_filter( 'wp_dropdown_users_args', 'add_subscribers_to_dropdown', 10, 2 );
function add_subscribers_to_dropdown( $query_args, $r ) {
    $query_args['who'] = '';
    return $query_args;
}
1

Just one thing I spent a couple of hours on worth mentioning. If you have custom capabilities, and you want to update a custom role capabilities, don't do it in the add_role function.

For example, this won't update the role as it already exists:

    $role = add_role(
        'client',
        'Client',
        array(
            'read' => true,
            'edit_posts' => true,
            'delete_posts' => false,
            CAP_READ_ADDRESS => true,
            CAP_DELETE_ADDRESS => true,
            CAP_EDIT_OTHER_ADDRESSES => true,
            CAP_READ_PRIVATE_ADDRESS => true,
            CAP_EDIT_ADDRESSES => true,
            CAP_PUBLISH_ADDRESS => true,
            CAP_EDIT_ADDRESS => true
        )

After any capabilities update, my client role won't get updated as it already exists.

The safest way to add custom capabilities to a custom role is to get the role and add them after add_role

function addCaps() {
    $role = get_role('client');
    $role->add_cap(CAP_READ_ADDRESS, true);
    $role->add_cap(CAP_EDIT_ADDRESSES, true);
}

add_action('admin_init', 'addCaps');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.