ashanrupasinghe
ashanrupasinghe

Reputation: 756

How to enable a custom post type to custom user role in WordPress

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' );
        }
        }

Upvotes: 0

Views: 2276

Answers (1)

Sunil Dora
Sunil Dora

Reputation: 1472

You may use this plugin for your custom post type and custom user role.

Hope this will helps you.

For more information,

Upvotes: 1

Related Questions