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.

Syntax


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 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
What are partials in SCSS (Sass)?SCSSHow states work in React?ReactThe Difference Between Arrays and Objects in JavaScriptJavaScriptHow to include CSS and JavaScript files in your WordPress plugin?WordPressHow to remove trailing characters from a string using JavaScript?JavaScriptrandom function in SCSS (Sass)SCSS