简体   繁体   中英

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

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

Hope this will helps you.

For more information,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM