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.

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. Please report any issues at Github. Download the zipball. It is translation ready, so if you want to send me some translations (there are only about 5 strings) I’d be happy to add them.
Enjoy! And let me know what you think in the comments.
Pingback: Nav Menu Roles Plugin | Pippins Plugins
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.
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!
Thanks Kathy,
Amazing plugin, what i have waiting for, since years. Will review it and update u.
Thanks again
Thanks for the kind words Hamzah. I’m glad it could be useful to you. Do let me know where you review it. Cheers.
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!
Thanks Paal. It has definitely been a handy add on for me and many others.
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.
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
What a great simple, but oh so useful plugin. Thank you!
Great job, Kathy.
How do I get a menu item to appear on both logged in and logged out menus?
Thanks.
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!
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
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
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?
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
This is great, Kathy!
Hello Kathy! you are truly Awesome. Wonderful Plugin! ur a Life saver. Keep up the good work!
Kathy, you are certainly awesome all right
This plugin is perfect for what i need ! Thankyou :)
This was just what I needed for my S2 Member wordpress site! Awesome is right!
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
This is not a support forum. The support forum is : http://wordpress.org/support/plugin/nav-menu-roles.