1

I am running Multisite, and would like to be able to append menu items to all frontend menus on all sites. This should work for menus saved via the admin interface as well as the default menu that is displayed.

This code works for saved menu items, but not the default menu:

add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );

function your_custom_menu_item ( $items, $args ) {
    $items .= '<li>my menu item</li>';
    return $items;
}

So my question is, how can I achieve functionality similar to that in the code above for the default front end menu? Ideally, I would be able to prepend/append the generated html for the menu items regardless of how they were generated (saved menu / default menu).

Alternatively, any information about how WP generates the default menu would be helpful.

I can say that the wp_nav_menu_times filter is not even triggered with the default menu. The function wp_nav_menu in wp_includes/nav_menu_template.php that applys the filter returns early (before most work including the application of filters) if no menu is found by wp_get_nav_menu_object.

1 Answer 1

0

add_filter( 'wp_page_menu', 'your_custom_menu_item', 10, 2 );

3
  • This solution is workable but there are two problems. 1 - this allows me to prepend/append the generated html for the entire menu, but not the menu items. I could do some fancy string modification, but this is less than ideal. 2 - This solution doesn't work for saved menus. Ideally, the solution would work regardless of how the menu is generated. Mar 27, 2012 at 17:55
  • I've accepted this, and implemented it. As mentioned I did have to slice up the generated html for the menu so I could append some items, but after searching around the codex and core, I don't see another option. Mar 29, 2012 at 18:59
  • Please try to extend your answer on how you think and why this snippet might address OP's issue. Code-only answers are discouraged.
    – leymannx
    Apr 25, 2019 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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