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.

This entry was posted in Plugins and tagged , . Bookmark the permalink. Both comments and trackbacks are currently closed.

40 Comments

  1. Jenny Ragan
    Posted September 20, 2012 at 6:23 pm | Permalink

    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.

    • Posted September 20, 2012 at 6:36 pm | Permalink

      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!

  2. Hamzah
    Posted October 2, 2012 at 4:31 pm | Permalink

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

    Thanks again

    • Posted October 2, 2012 at 5:26 pm | Permalink

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

  3. Posted October 29, 2012 at 12:42 pm | Permalink

    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!

    • Posted October 29, 2012 at 1:26 pm | Permalink

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

  4. Katya
    Posted December 12, 2012 at 1:43 am | Permalink

    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.

    • Posted December 12, 2012 at 2:12 am | Permalink

      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

  5. Jme
    Posted December 24, 2012 at 3:33 pm | Permalink

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

  6. Brian
    Posted December 31, 2012 at 2:11 pm | Permalink

    Great job, Kathy.

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

    Thanks.

    • Posted December 31, 2012 at 7:04 pm | Permalink

      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!

  7. komal
    Posted February 8, 2013 at 6:00 pm | Permalink

    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

    • Posted February 8, 2013 at 6:06 pm | Permalink

      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

  8. Posted February 11, 2013 at 6:39 pm | Permalink

    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?

    • Posted February 11, 2013 at 7:08 pm | Permalink

      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

  9. Posted February 25, 2013 at 11:54 pm | Permalink

    This is great, Kathy!

  10. Stanley
    Posted May 14, 2013 at 10:06 pm | Permalink

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

  11. Posted May 18, 2013 at 4:07 am | Permalink

    Kathy, you are certainly awesome all right

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

  12. Sue
    Posted May 27, 2013 at 6:47 pm | Permalink

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

  13. shikha
    Posted June 2, 2013 at 1:39 pm | Permalink

    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

  14. Pavels
    Posted September 14, 2013 at 6:35 am | Permalink

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

  15. Posted September 19, 2013 at 4:31 pm | Permalink

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

  16. Glen
    Posted October 25, 2013 at 7:17 am | Permalink

    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! :)

    • Posted October 25, 2013 at 12:47 pm | Permalink

      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.

  17. Posted November 5, 2013 at 11:52 am | Permalink

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

  18. Posted November 15, 2013 at 10:41 am | Permalink

    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?

    • Posted December 6, 2013 at 1:24 pm | Permalink

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

  19. Posted January 5, 2014 at 3:59 pm | Permalink

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

    • Posted January 5, 2014 at 10:06 pm | Permalink

      Hi Claus,

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

      Cheers,
      -k

  20. Posted January 31, 2014 at 8:01 pm | Permalink

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

    • Posted January 31, 2014 at 9:23 pm | Permalink

      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.

      • Posted January 31, 2014 at 9:46 pm | Permalink

        That explanation helps me a lot! Thank you!!

  21. Posted February 4, 2014 at 8:07 pm | Permalink

    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!

    • Posted February 5, 2014 at 9:57 am | Permalink

      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.

  22. Posted February 12, 2014 at 2:50 am | Permalink

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

  23. Posted February 12, 2014 at 11:32 am | Permalink

    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.

    • Posted February 12, 2014 at 12:00 pm | Permalink

      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

One Trackback