wordpressfunctionmenuitemrolescapability

add_menu_page item show to Editor Role Wordpress


I've been trying to add a menu to my WP Dashboard which I already accomplished. But now I want to show this menu to the Editor role as well as the administrator.

Here is my code

add_action( 'admin_menu', 'register_my_custom_menu_page' );
function register_my_custom_menu_page() { 
 add_menu_page( 'Instagram test', 'Instagram test', 'read', 'admin.php?page=sb-instagram-feed', '', 'dashicons-welcome-widgets-menus', 90 );
}

So far it works, but only for my administrator user, not to my Editors users.

I've read about the capabilities and that is why I put the read value on the function above.

How can I also show this menu to my Editors users?

Here is a screenshot, my custom menu is Instagram test.

Admin's Dashboard

enter image description here

Editor's Dashboard

Editors Dashboard


Solution

  • While I can't attest to why read isn't working - generally if you want to limit something by user roles, you can put in the slug for the role. If you read the source for add_menu_page() it will actually run the capability through current_user_can which accepts a role slug as well.

    I would replace read with editor and see what that gets you. It will also work for admins since it propagates down the list and administrators all have the editor, contributor, etc. "capabilities".

    Edit: It appears you have the Instagram Feed plugin installed which will be conflicting with your custom plugin. The code from that plugin shows the sb-instagram-feed page belongs to that plugin:

    function sb_instagram_menu() {
        add_menu_page(
            __( 'Instagram Feed', 'instagram-feed' ),
            __( 'Instagram Feed', 'instagram-feed' ),
            'manage_options',
            'sb-instagram-feed',
            'sb_instagram_settings_page'
        );
        add_submenu_page(
            'sb-instagram-feed',
            __( 'Settings', 'instagram-feed' ),
            __( 'Settings', 'instagram-feed' ),
            'manage_options',
            'sb-instagram-feed',
            'sb_instagram_settings_page'
        );
    }
    add_action('admin_menu', 'sb_instagram_menu');
    

    And that plugin requires manage_options, an administrator only capability. You'll need to not link to the page that other plugin makes, or deactivate that plugin.

    Edit 2: Note that editing plugin files directly usually isn't a great practice as any changes you make will be overwritten when the plugin is updated. You might be able to unhook the current admin menu for it and hook in your custom one.

    // Remove Existing Menu
    remove_action( 'admin_menu', 'sb_instagram_menu' );
    
    // Add Custom Menu
    add_action( 'admin_menu', 'custom_sb_instagram_menu');
    function custom_sb_instagram_menu() {
        add_menu_page(
            'Instagram Test',
            'Instagram Test',
            'editor',
            'sb-instagram-feed',
            'sb_instagram_settings_page'
        );
        add_submenu_page(
            'sb-instagram-feed',
            'Test Settings',
            'Test Settings',
            'editor',
            'sb-instagram-feed',
            'sb_instagram_settings_page'
        );
    }