sandywp / plugins / nav-menu-roles

Nav Menu Roles

4.9 out of 5 stars. 4.9 219 reviews

Hide custom menu items based on user roles.

Launch a sandbox with this plugin → No signup.
Ready in seconds.

v3.0.0 70,000+ installs WP 6.6.0+ PHP 7.4+

your-sandbox.sandywp.com/wp-admin
Nav Menu Roles running inside a SandyWP sandbox

What you get inside

1 screen
  • Show the new options for the menu items in the admin menu customizer

About this plugin

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.

Nav Menu Roles is very flexible. In addition to standard user roles, you can customize the functionality by adding your own check boxes with custom labels using the nav_menu_roles filter and then using the nav_menu_roles_item_visibility filter to check against whatever criteria you need. You can check against any user meta values (like capabilities) and any custom attributes added by other plugins.

IMPORTANT NOTE

In WordPress menu items and pages are completely separate entities. Nav Menu Roles does not restrict access to content. Nav Menu Roles is only for showing/hiding nav menu items. If you wish to restrict content then you need to also be using a membership plugin.

Usage

  1. Go to Appearance > Menus
  2. Set the “Target audience” to either “Everyone”, “Logged In Users”, or “Logged Out Users”. “Everyone” is the default.
  3. If you wish to customize by role, set the “Target audience” to “Logged In Users” and under “Target role” check the boxes next to the desired roles. The item will be visible to users who have any of the checked roles. Roles are not hierarchical, so an item set to “Subscriber” is not visible to Administrators unless you also check “Administrator”. If no roles are checked, the item is visible to all logged in users.
  4. Set the “Display Mode” to “Show” to display the item only to the selected audience, or to “Hide” to hide the item from the selected audience and show it to everyone else. “Show” is the default.

Support

Support is handled in the WordPress forums. Please note that support is limited and does not cover any custom implementation of the plugin. Before posting, please read the FAQ. Also, please verify the problem with other plugins disabled and while using a default theme.

Please report any bugs, errors, warnings, code problems to GitHub

Questions

I don’t see Appearance > Menus. Where are the settings?

Block themes, such as Twenty Twenty-Five, use the Navigation block instead of classic menus, so WordPress hides Appearance > Menus.

Since version 3.0.0, the Nav Menu Roles settings are in the Site Editor instead:

  1. Go to Appearance > Editor > Navigation and edit your menu.
  2. Select a link or submenu.
  3. In the block settings sidebar, open the “Nav Menu Roles” panel.

The settings work the same way as for classic menus. Hiding a submenu also hides all of the links inside it. Links generated automatically by the Page List block cannot be restricted.

Classic themes still use Appearance > Menus.

I don’t see the Nav Menu Roles options in Appearance > Menus?

Nav Menu Roles adds its options through the wp_nav_menu_item_custom_fields hook, which has been part of WordPress since 5.4. If a theme or plugin replaces the menu editor’s Walker and leaves that hook out, no plugin can add fields there.

To check, switch to a default theme and disable other plugins one at a time. If you find the conflict, ask its author to include the core wp_nav_menu_item_custom_fields hook. Visibility on the front end still works while the options are missing, so you can also switch themes temporarily to edit the settings, then switch back.

I’m using XYZ Membership plugin and I don’t see its “levels”?

There are apparently a few membership plugins out there that don’t use traditional WordPress roles/capabilities. My plugin will list any role registered in the traditional WordPress way. If your membership plugin is using some other system, then Nav Menu Roles won’t work with it out of the box. Since 1.3.5 I’ve added a filter called nav_menu_roles_item_visibility just before my code decides whether to show/hide a menu item. There’s also always been the nav_menu_roles filter which lets you modify the roles listed in the admin. Between these two, I believe you have enough to integrate Nav Menu Roles with any membership plugin.

Here’s an example where I’ve added a new pseudo role, creatively called “new-role”. The first function adds it to the menu item admin screen. The second function is pretty generic and won’t actually do anything because you need to supply your own logic based on the plugin you are using. Nav Menu Roles will save the new “role” info and add it to the item in an array to the $item->roles variable.

Existing Compatibility Plugins

  1. Wishlists Memberships
  2. WooCommerce Memberships

If your membership plugin is not listed here, you may be able to use the above bridge plugins as a template. Scroll down to the bottom of the main plugin file and you will see a section for “Helper Functions”. If you modify the 3 wrapper functions according to your membership plugin’s logic, the rest of the plugin should handle the integration with Nav Menu Roles.

Adding a new “role”

The roles in NMR are filterable distinct from the global $wp_roles. This allows for compatibility to be added between plugins that don’t use the core roles to determine access, like some membership plugins.

/**
 * Add custom roles to Nav Menu Roles menu list
 * param: $roles an array of all available roles, by default is global $wp_roles 
 * return: array
 */
function kia_new_roles( $roles ) {
  $roles['new-role-key'] = 'new-role';
  return $roles;
}
add_filter( 'nav_menu_roles', 'kia_new_roles' );

Note, if you want to add a WordPress capability the above is literally all you need. Because Nav Menu Roles checks whether a role has permission to view the menu item using current_user_can($role) you do not need to right a custom callback for thenav_menu_roles_item_visibility` filter.

In case you do need to check your visibility status against something very custom, here is how you’d go about it:

/**
  * Change visibility of each menu item
  * @param bool $visible
  * @param obj $item The complete menu object. Nav Menu Roles adds its info to the following keys {
  *         roles - Possible value: "in" (all logged in) | "out" (all logged out) | an array of specific roles, ex: [ "administrator", "editor" ]
  *         display_mode - Possible values: "show" (show if conditions met) | "hide" (hide if conditions met)
  * return boolean
  */
function kia_item_visibility( $visible, $item ) {
  if ( isset( $item->roles ) && is_array( $item->roles ) && in_array( 'new-role-key', $item->roles ) ) {
  /*  if ( // your own custom check on the current user versus 'new-role' status ) {
        $visible = true;
      } else {
        $visible = false;
    }
  */  }
  return $visible;
}
add_filter( 'nav_menu_roles_item_visibility', 'kia_item_visibility', 10, 2 );

Note that you have to generate your own if/then logic. I can’t provide free support for custom integration with another plugin. You may contact me to discuss hiring me, or I would suggest using a plugin that supports WordPress’ roles, such as Justin Tadlock’s Members.

Sort the roles alphabetically

Add the following snippet to your theme’s functions.php file:

/*
 * Sort the NMR roles
 * @param: $roles an array of all available roles with ID=>Name
 * @return: array
 */
function kia_sort_roles( $roles ) {
  if ( is_admin() ) {
    $array_lowercase = array_map( 'strtolower', $roles );
    array_multisort( $array_lowercase, SORT_ASC, SORT_STRING, $roles );
    return $roles;
  }
}
add_filter( 'nav_menu_roles', 'kia_sort_roles' );

The menu exploded? Why are all my pages displaying for logged out users?

If every item in your menu is configured to display to logged in users (either all logged in users, or by specific role), then when a logged out visitor comes to your site there are no items in the menu to display. wp_nav_menu() will then try check its fallback_cb argument… which defaults to wp_page_menu.

Therefore, if you have no items to display, WordPress will end up displaying ALL your pages!!

If you don’t want this, you must set the fallback argument to be a null string.

wp_nav_menu( array( 'theme_location' => 'primary-menu', 'fallback_cb' => '' ) );

You must do this for every effected instance of wp_nav_menu() in your theme templates. It is not possible for me to tell you where they are located, but header.php is a very common location for the main menu.

Alternatively, you could do this universally by adding the following snippet to your child theme’s function.php file or by adding it via the Code Snippets plugin:

/**
 * Disables the fallback page menu for all menus
 *
 * @param array $args Array of wp_nav_menu() arguments.
 * @return array
 */
function kia_nav_menu_args( $args ) {
  $args['fallback_cb'] = '';
  return $args;
}
add_filter( 'wp_nav_menu_args', 'kia_nav_menu_args' );

What happened to my menu roles on import/export?

The Nav Menu Roles plugin stores 1 piece of post meta to every menu item/post. This is exported just fine by the default Export tool.

However, the Import plugin only imports certain post meta for menu items. As of version 1.3, I’ve added a custom Importer to Nav Menu Roles as a work around.

4.9

219 reviews

  • 5 ★ 213
  • 4 ★ 2
  • 3 ★ 1
  • 2 ★ 0
  • 1 ★ 3

Ratings come from WordPress.org. SandyWP does not collect its own reviews.

sandywp / also on the shelf

Try another plugin

  • Admin Menu Editor

    4.6 · 300K+ installs

    Lets you edit the WordPress admin menu. You can re-order, hide or rename menus, add custom menus and more.

  • Max Mega Menu

    4.8 · 300K+ installs

    An easy to use mega menu plugin. Written the WordPress way.

  • Menu Image, Icons made easy

    4.5 · 100K+ installs

    Adds an image or icon in the menu items. You can choose the position of the image (after, before, above, below) or even hide the…

Spin up a real WordPress site in seconds.

Test plugins, build a demo, hand a client a link — then squash it and start again. No local setup, no Docker.