Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way to determine which theme placement a menu is for when modifying it with the wp_get_nav_menu_items filter hook?

I'd like to only add items to certain menus and I don't want to have to depend on the ID or the name of the menu.

share|improve this question
add comment

1 Answer

Not sure this is the simplest means, but you could conditionally add the filter to just the location you want to modify via wp_nav_menu_args filter when wp_nav_menu is called, then immediately remove the filter so it isn't applied to other menus.

function wpa108544_nav_menu_args( $args ){
    // check if it's the location we want the filter applied to
    if( 'primary' == $args['theme_location'] )
        add_filter( 'wp_get_nav_menu_items', 'wpa108544_get_nav_menu_items', 10, 3 );

    return $args;
}
add_filter( 'wp_nav_menu_args', 'wpa108544_nav_menu_args' );


function wpa108544_get_nav_menu_items( $items, $menu, $args ){
    // remove the filter
    remove_filter( current_filter(), __FUNCTION__, 10, 3 );

    // do your menu manipulation stuff here
}
share|improve this answer
    
To me that seems too much of a hack and creates brittle code. Why isn't the args from wp_nav_menu_args used for wp_get_nav_menu_items? –  Daniel A. White Aug 1 '13 at 11:44
    
Brittle in what way? Describe a scenario where this would not work for you. In some cases there are no nav menu args, wp_get_nav_menu_items isn't necessarily called via wp_nav_menu. –  Milo Aug 1 '13 at 15:11
    
Brittle because its just a very poor design. –  Daniel A. White Aug 1 '13 at 17:21
    
if you'd care to explain actual, practical issues with it, perhaps we can come up with a better solution. –  Milo Aug 1 '13 at 17:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.