WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Feb 23 2011

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]

Written by Travis Smith · Categorized: Plugins, Tutorials

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. Tobias says

    February 25, 2011 at 2:50 am

    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?

    Reply
    • Travis Smith says

      February 25, 2011 at 8:13 am

      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.

      Reply
      • Tobias says

        February 25, 2011 at 6:14 pm

        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.

        Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in