Site icon WP Smith

Creating Multiple Custom Menus in WordPress 3.1

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]