WordPress.org

Plugin Directory

Nav Menu Roles

Hide custom menu items based on user roles

I don't see the Nav Menu Roles options in the admin menu items?

This is because you have another plugin (or theme) that is also trying to alter the same code that creates the Menu section in the admin.

WordPress does not have sufficient hooks in this area of the admin and until they do plugins are forced to replace everything via custom admin menu Walker, of which there can be only one. There's a trac ticket for this, but it has been around a while.

A non-exhaustive list of known conflicts:

  1. UberMenu 2.x Mega Menus plugin
  2. Add Descendants As Submenu Items plugin
  3. Navception plugin
  4. Suffusion theme
  5. BeTheme
  6. Yith Menu
  7. Kleo Theme
  8. Jupiter Theme

Workaround #1

Shazdeh, the author of Menu Item Visibility Control plugin had the genius idea to not wait for a core hook and simply add the hook ourselves. If all plugin and theme authors use the same hook, we can make our plugins play together.

Therefore, as of version 1.6 I am modifying my admin walker to only adding the following line (right after the description input):

<?php 
// This is the added section
do_action( 'wp_nav_menu_item_custom_fields', $item_id, $item, $depth, $args );
// end added section 
?>

I am then adding my fields to this hook. Ask your conflicting plugin/theme's author to do the same and our plugins should become compatible.

Workaround #2

As a workaround, you can switch to a default theme (or disable the conflicting plugin), edit the Nav Menu Roles, for each menu item, then revert to your original theme/ reenable the conflicting plugin. The front-end functionality of Nav Menu Roles will still work.

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.

Adding a new "role"

/*
 * 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 visibilty of each menu item
 * param: $visible boolean
 * param: $item object, the complete menu object. Nav Menu Roles adds its info to $item->roles
 * $item->roles can be "in" (all logged in), "out" (all logged out) or an array of specific roles
 * 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.

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.

How Do I Use the Custom Importer?

  1. Go to Tools>Export, choose to export All Content and download the Export file
  2. Go to Tools>Import on your new site and perform your normal WordPress import
  3. Return to Tools>Import and this time select the Nav Menu Roles importer.
  4. Use the same .xml file and perform a second import
  5. No duplicate posts will be created but all menu post meta (including your Nav Menu Roles info) will be imported

Requires: 3.8 or higher
Compatible up to: 4.1.5
Last Updated: 2015-1-22
Active Installs: 40,000+

Ratings

4.9 out of 5 stars

Support

2 of 12 support threads in the last two months have been resolved.

Got something to say? Need help?

Compatibility

+
=
Not enough data

3 people say it works.
0 people say it's broken.

100,2,2 100,2,2 100,1,1
50,4,2
100,1,1 100,1,1
67,3,2 100,2,2
100,1,1 100,1,1
50,2,1
100,7,7
100,1,1
67,3,2 100,1,1 100,1,1
100,3,3 100,3,3
50,2,1
100,2,2
67,6,4 100,5,5
67,3,2
100,1,1
100,3,3