wordpresswordpress-roleswordpress-capabilities

capabilty in WordPress to show menu to all registered users?


I am creating a plugin and adding menu in dashboard WordPress

add_submenu_page('documents', 'Add Document', 'Add Document', 'manage_options','add_document', 'my_plugin_options3');

I also want to show that menu option to my registered users who are subscribers.but when I do it for subscribers menu only appear in subscriber dashboard and when I use manage_options it only appears for admin, not subscribers. I want to show it on both.

add_menu_page( 'My Plugin Options', 'docs Management', 'subscriber', 'documents', 'my_plugin_options','',4 );

I tried Summary of Roles reference from here


Solution

  • Subscribers and Admins are roles, you should try using a capability within those roles, like you did for the first sub menu page you added. Subscribers have the "read" capability, as does every other user role, so I would start there.

    add_menu_page( 'My Plugin Options', 'docs Management', 'read', 'documents', 'my_plugin_options','',4 );
    

    If you only want to show the menu for Subscribers and Admins, and no other role, you could create your own capability, then assign it to your menu, like so:

    <?php 
      global $wp_roles; 
      $wp_roles->add_cap( 'administrator', 'view_custom_menu' ); 
      $wp_roles->add_cap( 'subscriber', 'view_custom_menu' );
    
      add_menu_page( 'My Plugin Options', 'docs Management', 'view_custom_menu', 'documents', 'my_plugin_options','',4 );
    
    ?>