1

Who can help me with roles and capabilities? I am stuck on this and I need some help.

I want a role that only can add, remove edit etc. a specific CPT.

What I have so far

add_action('init', function(){  
    register_post_type( 'catalog',
        array(
            'capability_type'   => 'manage_catalog',
            'labels' => array(
                'name'          => 'Catalogus',
                'add_new'       => 'Nieuw item',
                'add_new_item'  => 'Nieuw item toevoegen',
                'new_item'      => 'Nieuw item',
            ),
            'public' => true,
            'query_var' => true,
            'show_ui' => true,
            'show_in_rest' => false,
            'menu_icon' => 'dashicons-store',
            'menu_position' => 2,
            'supports' => array(
                'title',
                'thumbnail',
            ),
        )
    );

add_action('load-themes.php', function(){
    global $pagenow;

    if('themes.php' == $pagenow && isset($_GET['activated'])){ 
        $role = get_role('subscriber');
        $role->add_cap('manage_catalog'); 
    }else{
        //Remove the capacity when theme is deactivate
        $role = get_role('subscriber');
        $role->remove_cap('manage_catalog');
    }
});

With the code above I can not see the CPT catalog in the menu on the admin side. What I am missing in my code?

1 Answer 1

0

There's no such hook named load-themes.php in WordPress. One of the hooks you can use to add custom capabilities is after_setup_theme.

Also, there's no need to use 'themes.php' == $pagenow && isset($_GET['activated']) condition there. What are you trying to achieve this way?

Try to use this code instead:

add_action( 'after_setup_theme', function() {
  $role = get_role( 'subscriber' );

  $singular = 'catalog';
  $plural = 'catalogs';

  $role->add_cap( "edit_{$singular}" ); 
  $role->add_cap( "edit_{$plural}" ); 
  $role->add_cap( "edit_others_{$plural}" ); 
  $role->add_cap( "publish_{$plural}" ); 
  $role->add_cap( "read_{$singular}" ); 
  $role->add_cap( "read_private_{$plural}" ); 
  $role->add_cap( "delete_{$singular}" ); 
  $role->add_cap( "delete_{$plural}" );
  $role->add_cap( "delete_private_{$plural}" );
  $role->add_cap( "delete_others_{$plural}" );
  $role->add_cap( "edit_published_{$plural}" );
  $role->add_cap( "edit_private_{$plural}" );
  $role->add_cap( "delete_published_{$plural}" );
} );

Your Answer

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

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