Custom fields for nav menu items

So, since 3.0 WordPress had support for creating navigation menus, however it lacks a lot of features that exists in other CMS platforms, one being the ability to hide / show menu items based on whatever condition. This is a much needed feature for a wide range of websites, for example you want to hide some pages of your site from specific user roles as they don’t have access to it, or perhaps hide them based on where the site visitor is currently on. This is why I developed the Menu Item Visibility Control plugin a while back, it gives you the ability to take control of where and when menu items should be displayed (or not), or in other words it’s just like Widget Logic plugin, but for menu items.

The problem

The plugin adds an option under each menu item to input the condition on which when that condition is met, the menu item should be visible (otherwise it is removed from menu on the front end), now WordPress does not provide any hooks for plugins to inject the custom options to each menu item. The only way to do that is by using the wp_edit_nav_menu_walker filter to change the “walker” WP uses to generate the menu items on the admin. This solution has been used by many plugins and themes and that’s where the problem begins, at a time only one class can be responsible for generating that form so whichever plugin or theme that ends up generating the form, only custom fields from that plugin or theme will be displayed. This issue is recognized by the Nav Menu Roles plugin and explained very well:

I don’t see the Nav Menu Roles options in the admin menu items?

This is likely because you have another plugin (or theme) that is also trying to alter the same code that creates the Menu section in the admin.

This is not a failure of Nav Menu Roles and there isn’t anything I can do about it. WordPress does not have sufficient hooks in this area of the admin and until they do plugins are forced to replace everything via custom admin menu Walker, of which there can be only one. Until these hooks are added the menu modification plugins are unfortunately going to conflict with one another.

Well put.

The solution

When I began the Menu Item Visibility Control plugin I researched other plugins that also added custom fields to menu items to find a miracle, and I found none. However in one of the plugins I reviewed (I can’t remember the name) instead of adding the fields directly inside the walker, the plugin author had only added an action hook to call for “wp_nav_menu_item_custom_fields” hook, and the custom fields from the plugin was added via that hook. I recognized this could be a great solution and that’s what I end up doing, later when I was working with Gantry framework I found out that that framework is also using the same technique, so there was no conflict and they were totally compatible. Later with Themify framework we used the same solution as well, and everything worked so far.

We have to wait for WP core to add the proper hooks (ticket) but until then I would like to ask my fellow WordPress developers to use the same solution and hook name to make sure themes and plugins can be compatible. I’m available if I can be of any assistance to you and I’m all ears if you have any ideas, comments, etc. :)


The connection

There’s an outdated joke that goes like this: before the invention of toilets, people used to build very wide shit-holes. If you asked why are they so wide you’d get an answer: “because we want to be able to pull out lambs that fall into the hole!”.

The pro-guns argument that “everyone would be safer if everyone had guns” follows a similar logic I think. But what the heck do I know.