How to restrict plugin’s sub-menu pages to admin/subscribers?

I am working on a plugin with 6 submenu pages. I want 5 of them to be accessible by Administrators only and 1 of them to be accessible by Subscribers only.

Both user roles will have different features available on their respective pages.

Read More

Is it possible to do that? If yes, how? If no, what are the alternatives?

Thanks for your time!

Related posts

Leave a Reply

4 comments

  1. Is this right:
    First 5 pages – Administrators ONLY, NO subscribers
    Sixth page – Subscriber ONLY, NO Administrator ?

    If this is correct than you only have to add the capability type as ‘administrator’ when creating the first 5, and ‘subscriber’ for creating the sixth. That should work. Or add a new capability for admin, use it while adding the first 5, and a new capability for subscriber, use it while adding the sixth.

  2. You should use capabilities in add_menu_page and add_submenu_page, for add_menu_page user read capability, for Administrators only sub-pages use manage_options cap and for Subscribers sub-page you can user read cap too or add new cap if you don’t want administrators see that page ( for capabilities easy management use Members Plugin )

  3. The correct way is to use capabilities, for example i have added one called “my_plugin” and check into the client role, so the client will see the menu with my_plugin capabilitie…for example:

    add_menu_page('Manage Clients', 'Manage Clients', 'my_plugin', 'manage-clients', 'clients_display_page');
    

    So then you go to Users->Roles, edit your role and check the my_plugin option, and those users will see the menu.

    This is correct way and will be easy to use and update.

  4. I may be late but here goes:

    function your_function_for_menus() { 
    
    add_submenu_page('slug_of_your_parent_menu', 'my title', 'your menu title','administrator', 'submenu_slug', 'function_to_display_this_submenu');
    
    add_submenu_page('slug_of_your_parent_menu', 'my title', 'your menu title','administrator', 'submenu_slug', 'function_to_display_this_submenu');
    
    add_submenu_page('slug_of_your_parent_menu', 'my title', 'your menu title','administrator', 'submenu_slug', 'function_to_display_this_submenu');
    
    add_submenu_page('slug_of_your_parent_menu', 'my title', 'your menu title','administrator', 'submenu_slug', 'function_to_display_this_submenu');
    
    add_submenu_page('slug_of_your_parent_menu', 'my title', 'your menu title','administrator', 'submenu_slug', 'function_to_display_this_submenu');
    
    add_submenu_page('slug_of_your_parent_menu', 'my title', 'your menu title','subscriber', 'submenu_slug', 'function_to_display_this_submenu');
    }
    

    This will create 6 submenus. Notice in the 6th one we have ‘subscriber’ instead of ‘administrator’ this will show that menu for subscribers ONLY not admins.

    you then use:

    add_action('admin_menu', 'your_function_for_menus');
    

    this will run the function to create the submenus.

    References:
    I was unable to post links due to anti-spam :(.

    so google
    wordpress capabilities
    wordpress add_action
    wordpress add_submenu_page

    they should explain everything.