RBFraphael
RBFraphael

Reputation: 499

Wordpress Custom Post Type and Custom User Role

I'm creating a project's plugin that needs two CPTs and a custom role to manage only that two CPTs. Creating the CPTs was the simple part, but I'm stuck on creating a custom user role for a week.

My CPTs are declared by:

register_post_type("cpt1", [
    'label' => "CPT 1",
    ...
    'capability_type' => "cpt",
]);
register_post_type("cpt2", [
    'label' => "CPT 2",
    ...
    'capability_type' => "cpt",
]);

And I'm declaring custom user role this way:

remove_role("cpt_manager");
add_role("cpt_manager", "CPT Manager", [
    'read' => true,
    'cpt' => true,
]);

But it's not working... I also tried declaring user role this way:

remove_role("cpt_manager");
add_role("cpt_manager", "CPT Manager", [
    'read' => true,
    'cpt' => true,
    'read_cpt1' => true,
    'edit_cpt1' => true,
    'publish_cpt1' => true,
    'delete_cpt1' => true,
    'read_cpt2' => true,
    'edit_cpt2' => true,
    'publish_cpt2' => true,
    'delete_cpt2' => true,
]);

But I can't make this to work.

Can someone help me pls?

Upvotes: 0

Views: 446

Answers (1)

Phil Veloso
Phil Veloso

Reputation: 1126

When registering you custom post type you need to define the capabilties in a more granulat manner, see documentation for more info.

'capabilities' => array(
  'edit_post'          => 'edit_cpt', 
  'read_post'          => 'read_cpt', 
  'delete_post'        => 'delete_cpt', 
  'edit_posts'         => 'edit_cpts', 
  'edit_others_posts'  => 'edit_others_cpts', 
  'publish_posts'      => 'publish_cpts',       
  'read_private_posts' => 'read_private_cpts', 
  'create_posts'       => 'edit_cpts', 
),

Upvotes: 0

Related Questions