Ever since WordPress Custom Menus were created and launched, I have been overly excited about it's potential. I have finally had some time where I could attempt to do something fun with them. However, it wasn't until the release of WordPress 3.1 that had the functionality that I was looking for. So I have developed a plugin that will allow you to filter out the custom menu instead of simply hiding portions of the menu through CSS {display:none;}.
This plugin can only be used in WordPress 3.0.5 if one change is made to a core WordPress file. However, I never advise making any changes or hacking to WordPress core on production environments. So if you make this change in production, do so at your own risk. That being said, in wp-includes folder, open the file nav-menu-template.php and add this line at line 199.
[php]$sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );[/php]
This line of code is already in WordPress 3.1 at line 201 hence why I don't mind sharing this. This line of code needs to appear after:
[php] // Set up the $menu_item variables
_wp_menu_item_classes_by_context( $menu_items );
$sorted_menu_items = array();
foreach ( (array) $menu_items as $key => $menu_item )
$sorted_menu_items[$menu_item->menu_order] = $menu_item;
unset($menu_items);[/php]
And before:
[php] $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
unset($sorted_menu_items);[/php]
Now you can access that filter via add_filter(); So,
[php]function wps_custom_nav_menu_items($args){
//do something
}
add_filter( 'wp_nav_menu_objects', 'wps_custom_nav_menu_items',10,3);[/php]
Tobias says
Hey Travis,
cool idea!
So I’ve tried your plugin and then I’ve been wondering about extending the idea to using shortcodes in menu items, in order to get more conditionals, like no-display on page-ids, or categories, etc, or automatic inclusion of child pages, or child categories or the like. So I tried this –
`add_filter(‘wp_nav_menu_objects’, ‘do_shortcode’);`
in order to add shortcode support at this point and pare shortcodes given in nav-item-descriptions – but it causes the page to not load correctly. I suppose it would be possible to add custom shortcode functionality to the plugin, but it would probably be better to use WP’s function.
Any idea why that wouldn’t work?
Travis Smith says
Tobias, I didn’t build in a shortcode functionality for the plugin yet; however, the Codex says, “If there are no shortcode tags defined, then the content will be returned without any filtering. This might cause issues if a plugin is disabled as its shortcode will still show up in the post or content.” I do plan adding further functionality as you are suggesting to the backend. Please let me know what you find out.
Tobias says
Hey Travis,
I managed to get it working. Now experimenting with on-the-fly element creation from standard db-requests. Will keep you posted how it goes.