1

I am create a custom user post type testimonial and want to give permission to user to edit and delete there post but its show some error.

1. I created user rule.

    function add_testimonial_management_role() {
     add_role('testimonial_manager',
                'Testimonial Manager',
                array(
                    'read' => true,
                    'edit_posts' => false,
                    'delete_posts' => false,
                    'publish_posts' => false,
                    'upload_files' => true,
                )
            );
       }
register_activation_hook( __FILE__, 'add_testimonial_management_role' );

2. Next added custom post

add_action( 'init', 'register_cpt_testimonials');
function register_cpt_testimonials() {
            $args = array(
            'label'               => __( 'testimonials', 'testimonials' ),
            'description'         => __( 'Testimonial', 'testimonials' ),
            'labels'              => $labels,
            'supports'            => array( 'title', 'comments', 'revisions', ),
            'hierarchical'        => false,
            'public'              => true,
            'show_ui'             => true,
            'rewrite'             => $rewrite,
                        'capability_type'     => array('testimonial','testimonials'),
                        'map_meta_cap'        => true,
        );
        register_post_type( 'testimonials', $args );
}

3. last i created rules

add_action('admin_init','add_role_caps',999);
    function add_role_caps() {

        // Add the roles you'd like to administer the custom post types
        $roles = array('testimonial_manager','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_testimonial');
                 $role->add_cap( 'read_private_testimonials' );
                 $role->add_cap( 'edit_testimonial' );
                 $role->add_cap( 'edit_testimonials' );
                 $role->add_cap( 'edit_others_testimonials' );
                 $role->add_cap( 'edit_published_testimonials' );
                 $role->add_cap( 'publish_testimonials' );
                 $role->add_cap( 'delete_others_testimonials' );
                 $role->add_cap( 'delete_private_testimonials' );
                 $role->add_cap( 'delete_published_testimonials' );

        }}

But its show you have error at end like this

Notice: Undefined variable: labels in /home/mortgag1/public_html/wp-content/themes/mortgage/functions.php on line 21

Notice: Undefined variable: rewrite in /home/mortgag1/public_html/wp-content/themes/mortgage/functions.php on line 26

Fatal error: Call to a member function add_cap() on a non-object in /home/mortgag1/public_html/wp-content/themes/mortgage/functions.php on line 43

Please help me anyone. thanks

0

i solve my issue my self by replacing some code as below

i replaced

   register_activation_hook( __FILE__, 'psp_add_project_management_role' );

To

add_action('admin_init','add_testimonial_management_role',998);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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