Yogesh Chauhan's Blog

How to modify navigation menus in WordPress?

in WordPress on February 21, 2021

We learned how to use add_filter to hook a function or a method to a specific filter action.

We also learned to use applt_filters to call the functions that have been added to the filter hook.

We are going to use the same applt_filters function to modify the menu in WordPress.

Side note: WordPress has another function wp_nav_menu to display the menu.


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

Where $items is the HTML list content for the menu items. It must be a string. $args is an object containing wp_nav_menu() arguments.

Example 1: This is how we can modify the menu.

function add_search_form($items, $args) {
          if( $args->theme_location == 'main-menu' ){
          $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);

Replace “main-menu” with your menu id in the code above. Adjust any other style if you want.

Example 2

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;

Same like the example before, replace “303” with your menu id in the code above. Adjust any other style if you want.

Adding the code 303 will replace it with whatever your menu id is.

Example 3: the simplest solution

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

function add_search_in_menu($items, $args) {
if( $args->theme_location == 'primary' )
       $items .= '<li class="search"><i class="fa fa-search"></i</li>';
       return $items;

You can attach the search form as well into the hook above. Then display it using JavaScript.

Credit goes to this page: WordPress wp_nav_menu_items hook

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
5 Steps to Create a Line using Canvas Tag in HTML5HTMLHow to detect the Blog Page in WordPress?WordPressWHERE Clause in PostgresPostgresHow to make flexbox items of the same size?CSSKilling A Project Part 2: Who should make the decision to kill a project?MiscellaneousHow to Draw a Text Image using JavaScript?JavaScript