YogeshChauhan . com

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

amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

Global JavaScript methods to convert variables to numbersJavaScriptHow to remove N/A from Radio Button list in Drupal?DrupalLearn to Make a Simple Contact Us Form using PHP and PDO-MySQLPHPHow to Skip or Exclude a Specific URL or File From .htaccess Rewrite Rule?PHPSelector Lists and Combinators in SCSS (Sass)SCSSHow to scroll contents of a an element vertically using JavaScript?JavaScript