apply_filters( ‘wp_nav_menu_items’, string $items, stdClass $args )

Filters the HTML list content for navigation menus.


See also


The HTML list content for the menu items.
An object containing wp_nav_menu() arguments.
More Arguments from wp_nav_menu( … $args )Array of nav menu arguments.
  • menu int|string|WP_Term
    Desired menu. Accepts a menu ID, slug, name, or object.
  • menu_class string
    CSS class to use for the ul element which forms the menu.
    Default 'menu'.
  • menu_id string
    The ID that is applied to the ul element which forms the menu.
    Default is the menu slug, incremented.
  • container string
    Whether to wrap the ul, and what to wrap it with.
    Default 'div'.
  • container_class string
    Class that is applied to the container.
    Default ‘menu-{menu slug}-container’.
  • container_id string
    The ID that is applied to the container.
  • container_aria_label string
    The aria-label attribute that is applied to the container when it’s a nav element.
  • fallback_cb callable|false
    If the menu doesn’t exist, a callback function will fire.
    Default is 'wp_page_menu'. Set to false for no fallback.
  • before string
    Text before the link markup.
  • after string
    Text after the link markup.
  • link_before string
    Text before the link text.
  • link_after string
    Text after the link text.
  • echo bool
    Whether to echo the menu or return it. Default true.
  • depth int
    How many levels of the hierarchy are to be included.
    0 means all. Default 0.
    Default 0.
  • walker object
    Instance of a custom walker class.
  • theme_location string
    Theme location to be used. Must be registered with register_nav_menu() in order to be selectable by the user.
  • items_wrap string
    How the list items should be wrapped. Uses printf() format with numbered placeholders. Default is a ul with an id and class.
  • item_spacing string
    Whether to preserve whitespace within the menu’s HTML.
    Accepts 'preserve' or 'discard'. Default 'preserve'.


$items = apply_filters( 'wp_nav_menu_items', $items, $args );



User Contributed Notes

  1. Skip to note 4 content

    Add HTML5 search form to a menu
    Add this code to functions.php. You can completely modify the output, and style the form as you want.
    Please, don’t forget to replace the theme_location !
    For example, if the id of my menu was main-menu, I would write if( $args->theme_location == 'main-menu' )
    Hope this will help.

    function add_search_form($items, $args) {
              if( $args->theme_location == 'menu-1' ){
              $items .= '<li class="menu-item">'
                      . '<form role="search" method="get" class="search-form" action="'.home_url( '/' ).'">'
                      . '<label>'
                      . '<span class="screen-reader-text">' . _x( 'Search for:', 'label' ) . '</span>'
                      . '<input type="search" class="search-field" placeholder="' . esc_attr_x( 'Search …', 'placeholder' ) . '" value="' . get_search_query() . '" name="s" title="' . esc_attr_x( 'Search for:', 'label' ) . '" />'
                      . '</label>'
                      . '<input type="submit" class="search-submit" value="'. esc_attr_x('Search', 'submit button') .'" />'
                      . '</form>'
                      . '</li>';
            return $items;
    add_filter('wp_nav_menu_items', 'add_search_form', 10, 2);
  2. Skip to note 5 content

    Add extra menu item to a WordPress Nav Menu

    Using this code snippet, one can add an additional menu item to the end of a WordPress navigation menu. For example, here in this case I am adding a WooCommerce Login/Register link when a user is not logged in, and a My Account page link when a user is logged in.

    add_filter( 'wp_nav_menu_items', 'add_extra_item_to_nav_menu', 10, 2 );
    function add_extra_item_to_nav_menu( $items, $args ) {
        if (is_user_logged_in() && $args->menu == 303) {
            $items .= '<li><a href="'. get_permalink( get_option('woocommerce_myaccount_page_id') ) .'">My Account</a></li>';
        elseif (!is_user_logged_in() && $args->menu == 303) {
            $items .= '<li><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Sign in  /  Register</a></li>';
        return $items;

    How to use the above code?

    1. If you are using a child theme, then add this snippet to your currently active child theme’s functions.php file, otherwise, add to your currently active theme’s functions.php file. Alternatively, you can use a code snippet plugin like Code Snippets ( without directly modifying your theme files.
    2. You can use theme_location or the ID of your nav menu in the if condition to specify the menu which you are adding the item to. I have used ID, if you are going to use the ID too, then change it from 303 to whatever the ID of your menu is. You can find the ID of your menu in the WP Admin panel by going to Appearance > Menus. Then select a menu from the drop-down that says: Select a menu to edit and click on the select button next to it. Now you will see the ID of your menu at the end of your current screen URL like this: You can see the 303 at the end, which is the ID of my menu, yours can be something different, replace with that.

    Hope this helps someone :)

  3. Skip to note 6 content

    Used the following snippet to add a user avatar image as the last menu item. Had to use $args->menu==='main-menu' rather than 'primary' or id, as other examples did. Found this out finally by printing out $args->menu to the navbar, to see what that argument actually was.

    // register shortcode
    add_shortcode('bbwp_get_session', 'bbwp_get_session'); 
    add_filter( 'wp_nav_menu_items', 'add_extra_item_to_nav_menu', 10, 2 );
    function add_extra_item_to_nav_menu( $items, $args ) {
    	if (is_user_logged_in() && $args->menu === 'main-menu') {
    		$items .= '<li class="wt_menu_item_user_avatar"><a href="/my-account">' . get_avatar( $current_user->ID, 32 ) . '</a></li>';
    	return $items;

You must log in before being able to contribute a note or feedback.