Filter Hook since 3.0.0
wp_nav_menu_items
Filters the HTML list items of a rendered nav menu
Description
wp_nav_menu_items lets you append or alter menu markup without editing the menu in the admin — useful for injecting a login/cart link at the end.
When it runs
Inside wp_nav_menu(), after the menu tree is walked into <li> markup, before it's echoed or returned.
Signature
apply_filters( 'wp_nav_menu_items', string $items, stdClass $args );Parameters
$itemsstring — HTML list items for the menu.$argsstdClass — Arguments passed to wp_nav_menu().
Examples
Basic
add_filter( 'wp_nav_menu_items', function( $items, $args ) {
if ( 'primary' !== $args->theme_location ) {
return $items;
}
$items .= is_user_logged_in()
? '<li><a href="' . wp_logout_url() . '">Log out</a></li>'
: '<li><a href="' . wp_login_url() . '">Log in</a></li>';
return $items;
}, 10, 2 );Append a login/logout link to the primary menu only.
Common Use Cases
- Append a login/cart/search item to a menu
- Inject a CTA button into navigation
- Conditionally add items by user role
Common mistakes
- Forgetting to check $args->theme_location — the callback then appends to every registered menu on the site, not just one
Related hooks
FAQ
How do I target only one specific menu?
Check $args->theme_location (or $args->menu) inside the callback and return $items unchanged for every other menu.
Source: wp-includes/nav-menu-template.php