Custom WordPress and WooCommerce Development

Nav Menu Roles Plugin

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.


Comments

41 responses to “Nav Menu Roles Plugin”

  1. […] can read more about the plugin on Kathy’s website. Download plugin Tweet Follow @pippinsplugins Kathy […]

  2. Jenny Ragan Avatar
    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. 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. Thanks Kathy,
    Amazing plugin, what i have waiting for, since years. Will review it and update u.

    Thanks again

    1. 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. Hey Kathy!

    The plugin looks very interesting! It seems like a standard feature that should be added to the WP core.

    Have a great day!

    1. Thanks Paal. It has definitely been a handy add on for me and many others.

  5. 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. 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

  6. What a great simple, but oh so useful plugin. Thank you!

  7. Great job, Kathy.

    How do I get a menu item to appear on both logged in and logged out menus?

    Thanks.

    1. 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!

  8. 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. 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

  9. 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. 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

  10. This is great, Kathy!

  11. Stanley Avatar
    Stanley

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

  12. Kathy, you are certainly awesome all right

    This plugin is perfect for what i need ! Thankyou :)

  13. This was just what I needed for my S2 Member wordpress site! Awesome is right!

  14. shikha Avatar
    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

    1. This is not a support forum. The support forum is : http://wordpress.org/support/plugin/nav-menu-roles.

  15. thank you very much for this plugin, you are awesome, Kathy!

  16. Great plugin!! Thanks a lot! Easy to use.

  17. 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. 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.

  18. That’s brilliant Kathy! Just what I needed to work with S2 Members today – thank you!

    1. You’re very welcome. If you like it don’t forget to leave a positive review on WordPress.org

  19. 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?

    1. Nav Menu roles does now let you roll your own integration for non-standard levels/roles, see the .

  20. Hi Kathy, thanks a lot for this great plugin. You made my day :-)

    1. Hi Claus,

      Thanks for dropping by. I’m pleased that my plugin was able to help you.

      Cheers,
      -k

  21. 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. 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.

      1. That explanation helps me a lot! Thank you!!

  22. 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. 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.

  23. My goodness – You are Awesome! Thanks! It is super easy – with lots of cool choices and works great with my membership plugin.

    1. Glad you like it. Could you leave your review on the WordPress site?

  24. 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.

    1. Hi Mohamed,

      Support is handled at the official WordPress forums. However, this issue is covered by both the Please Read before Posting thread in the WordPress Forums and in the FAQ.

      Cheers,
      -Kathy