Nav Menu Roles Plugin

Display or hide menu items based on user role

Over the weekend I was working on a project and my client needed to be able to hide certain menu links from non-logged in users.  In the past I have just created 2 menus, one for logged in users, and another for logged out users.  Not the most convenient thing ever, but simple enough.  However, since this was at least the second, and possibly the third time this has come up in various projects I decided to dig further.

Adding fields to the menu item back-end

This was the trickiest part really, because there aren’t any action hooks in this part of the code.  I have requested one with the following Trac Ticket, but until that gets added to we have to do it in a slightly less efficient way.

Thanks to this WordPress Answer I found that the whole menu item form is created by the Walker_Nav_Menu_Edit Walker, but that tucked away in the code is the ability to filter this and send a new Walker via the wp_edit_nav_menu_walker filter.  Actually this answer got me about 80% of the way there so I can hardly claim to be clever or original.  I just packaged it up.  Couples with this Answer about front-end menu Walkers, the first beta draft of my plugin was born.  At this point it really only handled different roles.

Nav Menu Roles screenshot
New options for menu settings allow you to customize who can view particular menu items

Before it was even added to the WordPress repository, Pippin Williamson did a quick video review, so I guess I am not the only person who has needed this ability from WordPress.  Pippin also had the cool idea to add the ability to show an item to all logged-in users, or all logged-out users, so the video screenshots will look different from the end result of version 1.0.  I think this was a cool addition, so thanks Pippin for the idea and for helping me work on it.

Basic Usage

Install and activate the plugin like normal. Create a custom menu and add items to it just like you usually would. You will now see the extra fields shown in the screenshot. From here you select to show the item to all logged in users, all logged out users… or to customize by role. If you chose customize by role, then you can use the checkboxes to determine which roles should be allowed to see this particular menu item. If you choose customize by role and do not check any roles, then the item will show to everyone just like it used to. There isn’t any more to it than that. It should be pretty straight-forward.

Download

Now available in the WordPress Repo.

Also available via my Github repo.

Support

Support is limited to bug-fixing and basic implementation. I can’t help with any customization.

Please report any bugs at Github’s issue tracker.  

Support requests are best handled in the Support Forums. Please don’t post support questions in the comments!

Enjoy! And let me know what you think.

41 thoughts on “Nav Menu Roles Plugin

  1. Pingback: Nav Menu Roles Plugin | Pippins Plugins

  2. Jenny Ragan

    I am djrmom from the WordPress Answer you linked to and I am very excited to see it put to use. I answered the question for the challenge of it but have never had any reason to actually use the code.

    1. kathy Post author

      Hi Jenny! Thanks for stopping by… and definitely extra thanks for that WordPress Answer. Once I had that the solution was relatively trivial. I’m already using it on a live site and know a few others that are too. WordPress rules!

  3. Hamzah

    Thanks Kathy,
    Amazing plugin, what i have waiting for, since years. Will review it and update u.

    Thanks again

    1. kathy Post author

      Thanks for the kind words Hamzah. I’m glad it could be useful to you. Do let me know where you review it. Cheers.

  4. Katya

    Thank you soooo much for creating this plugin!! It worked like a charm. I also have had to constantly create a custom menu for logged in or logged out users but this was the best plugin I could have found. Thank you!!! Its much appreciated.

    1. kathy Post author

      Katya,
      You’re welcome! I’m pretty sure it is the only plugin that does this. :) Glad it could help you and thanks for stopping by.
      -k

    1. kathy Post author

      As per the instructions, if you don’t check any boxes, the menu item will be visible to all users like normal. Thanks for dropping by!

  5. komal

    Hey Kathy,

    I have tried to use it with 3.4.1 and 3.5.1 with S2 Member plugin. It is getting installed without any error but is not showing up anywhere in menu section. Please let me know the possible solution.

    Thanks, Komal

    1. kathy Post author

      Komal,

      I’ve replied to your thread in the WP forums, have a bit of patience! :) But for anyone else with the same/similar issues, WordPress does not have the appropriate hooks required. When core is updated, I will update my plugin to not use a custom Walker for the admin menu items. Until then, my plugin is not compatible with any other plugin attempting to override the same Walker…. which I suspect is what S2 is doing. Basically, “there can be only one”, Walker and it looks like mine is losing out to S2’s.

      -k

  6. Finch

    Nice. This plugin is really damn useful. Btw, can this also restrict users to the pages even if the user knows the exact permalink of the page that he or she doesn’t have the right to view?

    1. kathy Post author

      Finch,

      Glad you like the plugin! But no, it only toggles the menu state, this plugin doesn’t restrict access to any content… that’s more suited to a membership plugin.

      Good luck,
      -k

  7. Stanley

    Hello Kathy! you are truly Awesome. Wonderful Plugin! ur a Life saver. Keep up the good work!

  8. shikha

    your plugin does not work on my website.. its not hiding the menu item in menu bar.. I used all logged in feature but before login. visitors are accessible to use it.. can you plz help me

  9. Glen

    Kathy in a word you are a LEGEND!!!!!! I can see you have received a lot of positive feedback here which is well deserved. It is members of the WordPress community like you that make this such an interesting platform for both developers and unskilled gurus alike. Here you have developed something which is elegant and simple. THANKYOU! :)

    1. kathy Post author

      Thank you for the kind words and the ego boost! I especially needed those today as my current code does not feel elegant or simple at the moment.

  10. Kim Coleman

    I’ve been using this plugin on a lot of our sites lately and wanted to see about extending it to incorporate the membership levels as set up by our plugin. Are there hooks to extend or a best practice for looping in your code?

  11. Glen Clay

    Is there a way to make the Nav Menu Roles compatible with the enfold theme? You have a great plugin and they have a great theme that don’t seem to get along. :)

    1. kathy Post author

      I don’t know anything about the Enfold theme. However, NVR is compatible with any theme that is using wp_nav_menu() on the front and isn’t adding their own Walker in the admin section. Unfortunately WP has yet to add the appropriate hooks to the admin side that allow my fields to be added without the more nuclear, Walker, total replacement approach.

  12. Seth

    Hi there –

    I really like your plugin. I wonder if you can guide me to a solution to help incorporate it into my real estate theme. When logged in, my new logged-in “employee menu” doesn’t immediately appear on the homepage. Occasionally it will pop-up on the homepage if I click away from the main page, and then return. I haven’t been able to discover a rhyme or reason to this. The employee menu does appear on the other pages, though. Thanks for any help you can provide!

    1. kathy Post author

      I suspect your theme is at fault (or a caching plugin maybe?), so try looking at the code that is generating the menu… commonly in header.php. But if you can reproduce the error in a default theme then please post an issue in the forums.

  13. Mohamed Salman

    Hi Kathy, This is really Nice Plugin. But i found some issue after installing this. I don’t know whether it is not supporting because of my theme or this plugin has not been installed properly.
    I would like to install this plugin in a website called educationhindustan.com, where user can see posts on subjects only when they are registered.
    But after installing it also, it doesn’t show me the “display mode” and “access role” option under category.
    Please help me in fixing this.
    Thanks in advance.

Comments are closed.