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

  • $items string — HTML list items for the menu.
  • $args stdClass — 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

Group wizard

One session: this hook, then each related_hooks entry. Walk it and tell us what to change.

wp_nav_menu_items → wp_footer

functions.php — wp_nav_menu_items
functions.php
PHP wp_nav_menu_items · Hook 1 of 2 · Step 1 of 6

Practice the wp_nav_menu_items WordPress filter hook in our interactive sandbox — a VS Code-style editor with a step-by-step wp_nav_menu_items tutorial, add_filter exercises, and instant feedback. No local WordPress install required.