Plugin update 2014/07/26

So, I updated a couple of my plugins which I think are cool to mention:

  • Crazy Pills: is now compatible with the new TinyMCE, and also has support for i18n.
  • Menu Item Visibility Control: there was a PHP notice in the admin dashboard which was ugly, and is fixed.
  • Widget Shortcode: added filters for default widget args, and also now you can change the title set for the widget via the “title” parameter of the shortcode
  • Widgets in Columns: this one was long overdue, there was a bug with the column display which is fixed, and the plugin uses the new media uploader in WP. I’ve always hated the name of the plugin, I think for the next update of this plugin I’ll rename it and send it to the repo.

Last but not least I have a new plugin: Snippets Plus, this is a cool little plugin based on the Snippets plugin by Justin Tadlock, the idea is first you add an instance of the Snippets Plus widget to any widget area, then you can change the content for that widget inside each post / page. It’s very useful for content-heavy websites.

Anyhoo, I’m brewing several new plugins and doing more updates, remember that your comments is my only fuel so please rate the plugins and leave any ideas, suggestions and stuff.

Images in menu items

One of the questions that comes up often in the forum is how the logo image can be added to the site’s navigation menu. It’s a cool effect but menus in WordPress are somewhat limited in what and how you can put in. The solution I always offered was using CSS image replacement technique to kick the text of one of the menu items out of the page and display a background image in place of it, you can see examples of this here, here, and here (all taken from the showcase page). Until one of users suggested using “img” tags inside the title of the navigation menu, I didn’t believe at first but after testing it turns out it works! I’m not sure if it’s a bug in WordPress or not (WP obsessively sanitizes user inputs) but, you know, after years of working with WP I’m glad it still can surprise me. :)

The only drawback is in the menus manager you can see the tag itself which is somewhat ugly, but it’s easier than CSS image replacement.

Images in menu items

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.