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.
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.
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.
Now available in the WordPress Repo.
Also available via my Github repo.
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.