apply_filters( 'wp_nav_menu_items', string $items, stdClass $args )

Filters the HTML list content for navigation menus.


Description Description

See also See also


Top ↑

Parameters Parameters

$items

(string) The HTML list content for the menu items.

$args

(stdClass) An object containing wp_nav_menu() arguments.


Top ↑

Source Source

File: wp-includes/nav-menu-template.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by LebCit

    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 2 content
    Contributed by arifktk

    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 (https://wordpress.org/plugins/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: https://myexampleshopsite.com/wp-admin/nav-menus.php?action=edit&menu=303. 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 :)

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