2

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.

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

Thanks for your time!

4 Answers 4

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 )

0

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.

-1

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.

3
  • Your answer is wrong. The add menu and submenu page functions use capabilities not roles. Administrator is a role manage-options, edit-posts etc are capabilities.
    – Chris_O
    Commented May 27, 2012 at 8:37
  • Chris, it is perfectly valid and you can use roles, with-in the codex they even give an example using roles. codex.wordpress.org/Function_Reference/add_menu_page#Examples Commented Jun 7, 2012 at 17:30
  • The codex was wrong! I just updated it.
    – Chris_O
    Commented Jun 7, 2012 at 18:10

Your Answer

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

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