0

I create a plugin with 6 submenu pages. I need 4 of them to be accessible by administrator and a custom user role i created say 'coach' user. Then the 2 of them to be accessible by administrator and another custom user role 'player'. How would i do that to add 2 user role?

3

The easiest way is create 2 custom capabilities and assign both to administrators, one to role coach and one to role player.

When you create your roles you do something like this:

$coach_caps = array(
  'read' => true,
  'something_else' => true,
  'can_open_coach_menu' => TRUE // this is important for your scope
);
add_role( 'coach', 'Coach', $coach_caps );

$player_caps = array(
  'read' => true,
  'something' => true,
  'can_open_player_menu' => TRUE // this is important for your scope
);
add_role( 'player', 'Player', $player_caps );

Then assign both capabilities to the administrator role:

$roles = new WP_Roles();
$roles->add_cap( 'administrator', 'can_open_coach_menu', TRUE);
$roles->add_cap( 'administrator', 'can_open_player_menu', TRUE);

Now when you add submenu pages:

// this is for administrator and coach roles
add_submenu_page( $parent, $p_title, $m_title, 'can_open_coach_menu', 'submenu1');

// this is for administrator and player roles
add_submenu_page( $parent, $p_title, $m_title, 'can_open_player_menu', 'submenu2');

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.