Managing Menu Item visibility in Wordpress

Published by Navdeep Singh on

For many websites, there probably isn’t much need to customize navigation. However, if you happen to run a website with multiple user types and access privileges, menu customization is a must.

Here are some of the reasons you may want to do this:

  • You offer members-only access to certain pages, content, and personal profile pages.
  • You accept guest posts and want to give them an easy way to log in without having to show a Login button to other visitors.
  • You have multiple user types that visit your website and you need to restrict access to certain parts of the navigation based on the role they play (i.e. administrator, editor, contributor, customer, etc.)
  • You run a forum or other discussion board and want to give logged-in users access to the private discussion area.
  • You want to include a registration link only to users who are not logged in or otherwise have access to your website.

looking at the problem the first idea that strikes an individual’s mind is using two separate menus, but to save duplication, one should try to manage one nav menu for different user roles.

1)Using separate menus :

In your WordPress Dashboard navigate to Appearance>Editor>functions.php. Now add the code below to the functions.php file:

function my_wp_nav_menu_args( $args = '' ) {

    if ( is_user_logged_in() ) { 
        $args['menu'] = 'logged-in';
    } else { 
        $args['menu'] = 'logged-out';
    }

    return $args;
}
add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );

This code simply directs our theme to display separate menus for different user roles.Now simply edit the ‘logged-in’ and ‘logged-out’ menus for the different views required.

Though the above approach is simple, one has to create different menus for different user roles, therefore, using the above approach is not recommended.

2)Using different WordPress plugins:

The task can be easily done using any one of the below plugins:

a)If Menu:

If Menu plugin adds extra functionality for menu items, making it easy to hide or display them based on user-defined rules. Example:

  • Display a menu item only if current User is logged in
  • Hide menu items if visiting from mobile device
  • Display menu items just for Admins and Editors

The plugin is easy to use, each menu item will have a “Change menu item visibility” option which will enable the selection of rules.

Installation Instructions: To install the plugin, follow the steps below

  1. Upload if-menu to the /wp-content/plugins/ directory
  2. Activate the plugin through the ‘Plugins’ menu in WordPress
  3. Enable visibility rules for your Menu Items in Appearance -> Menus page

b)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.

Nav Menu Roles is very flexible. In addition to standard user roles, you can customize the functionality by adding your own checkboxes 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.

USAGE

  1. Go to Appearance > Menus
  2. Set the “Display Mode” to either “logged in users”, “logged out users”, or “everyone”. “Everyone” is the default.
  3. If you wish to customize by role, set the “Display Mode” to “Logged In Users” and under “Restrict menu item to a minimum role” check the boxes next to the desired roles. Keep in mind that the role doesn’t limit the item strictly to that role, but to everyone who has that role’s capability. For example, an item set to “Subscriber” will be visible to Subscribers and by admins. Think of this more as a minimum role required to see an item.

Installation Instructions

  1. Upload the plugin folder to the /wp-content/plugins/ directory
  2. Activate the plugin through the ‘Plugins’ menu in WordPress
  3. Go to Appearance > Menus
  4. Edit the menu items accordingly. First, select whether you’d like to display the item to Everyone, all logged out users, or all logged in users.
  5. Logged in users can be further limited to specific roles by checking the boxes next to the roles you’d like to restrict visibility to.

FAQ

1)I don’t see different Role 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.

A non-exhaustive list of known conflicts:

  1. UberMenu 2.x Mega Menus plugin (UberMenu 3.x supports NMR!)
  2. Add Descendants As Submenu Items plugin
  3. Navception plugin
  4. Suffusion theme
  5. BeTheme
  6. Yith Menu
  7. Jupiter Theme
  8. iMedica theme
  9. Prostyler EVO theme
  10. Mega Main Plugin

 

 

 

 


Leave a Reply