How to modify navigation menus in WordPress?
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.

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


Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript Mar 3 Window setInterval() Method in JavaScript Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS

You might also like these

How to refresh a page using PHP at regularly occurring intervals?PHPHow to get start index and end index (as int) of substring in Swift?SwiftHow services and dependency injection work in Angular?AngularA Step by Step Guide to Make RSS in XML For Any Website or Blog For FreeMiscHow to use HTML picture Tag?HTMLJavaScript Input Validation Theme Park Example using throw StatementJavaScript