capabilities - add_menu_page() for more than one user role

admin2025-04-20  0

If I add a custom page to the wordpress backend menu via

add_menu_page('My Custom Page', 'My Custom page', 'editor', 'custom_page','add_custom_page','dashicons-admin-comments',8);

only a user with editor privileges can see the page in its menu. How can I make this menu entry also available for users with administrator privileges? Have I to use add_menu_page() for every user role seperately?

If I add a custom page to the wordpress backend menu via

add_menu_page('My Custom Page', 'My Custom page', 'editor', 'custom_page','add_custom_page','dashicons-admin-comments',8);

only a user with editor privileges can see the page in its menu. How can I make this menu entry also available for users with administrator privileges? Have I to use add_menu_page() for every user role seperately?

Share Improve this question edited Jul 18, 2018 at 9:38 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jul 6, 2018 at 9:44 User95User95 451 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 6

Let's take a look at Codex page for add_menu_page...

Third param is:

$capability (string) (Required) The capability required for this menu to be displayed to the user.

And later on in Notes section:

This function takes a ‘capability’ (see Roles and Capabilities) which will be used to determine whether or not a page is included in the menu. The function which is hooked in to handle the output of the page must check that the user has the required ‘capability’ as well.

As you can see, you should use capability (for example manage_options, publish_posts) and not user role (editor, subscriber) as that param.

So if you'll pass publish_pages as third param, only Editor, Administrator, and Super Admin will get access to that page.

Here you can find list of default capabilities for default user roles.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745114388a285774.html

最新回复(0)