CodeRows
CodeRows

Reputation: 1033

Make wordpress admin menu item only for admin users

Im adding a sub-menu under WordPress admin menu for my plugin

add_submenu_page(
        'demo',       // parent slug
        'Demo Settings',    // page title
        'Demo Settings',             // menu title
        'manage_options',           // capability
        'demo_settings',      // slug
        'show_demo_settings' // callback
    ); 

and this is adding a menu item which is available for all users, all level of users, I want to make it visible for only administrators, searched quite a bit but not found any solution in their documentation.

any one worked on this kind of problem?

Upvotes: 0

Views: 3362

Answers (2)

CodeRows
CodeRows

Reputation: 1033

roles is the trick, set the appropriate capability for the level of users you want to show the menu items

https://codex.wordpress.org/Roles_and_Capabilities

so a menu that need to be displayed to editors would have capability "edit_pages"

add_submenu_page(
        'demo',       // parent slug
        'Demo Settings',    // page title
        'Demo Settings',             // menu title
        'edit_pages',           // capability
        'demo_settings',      // slug
        'show_demo_settings' // callback
    );

the menu required for only admins would be with this capability "manage_options"

add_submenu_page(
        'demo',       // parent slug
        'Demo Settings',    // page title
        'Demo Settings',             // menu title
        'manage_options',           // capability
        'demo_settings',      // slug
        'show_demo_settings' // callback
    ); 

Upvotes: 2

Randy
Randy

Reputation: 9819

https://wordpress.org/plugins/nav-menu-roles/

This plugin lets you hide custom menu items based on user roles. So if you have a link in the menu that you only want to show to logged in users, certain types of users, or even only to logged out users, this plugin is for you.

Upvotes: 0

Related Questions