How to add extra menu item in WordPress Navigation menu

WordPress very easy interface for managing navigation for WordPress themes.  But in some cases you may want to add extra menu item in the WordPress navigation. Here is a simple example to show how to add extra menu item in WordPress navigation menu.

<?php
function custom_add_login_logout_link( $items, $args  ) {

    if( 'primary' == $args->theme_location ) {
            if ( is_user_logged_in() ) {

                $items .= '<li><a class="btn btn-logout" href="'.wp_logout_url(home_url()).'">Logout</a></li>';

            } else {

                $items .= '<li><a class="btn btn-login" href="'.wp_login_url() .'">Login</a></li>';

            }
        }
        return $items;

}
add_filter( 'wp_nav_menu_items', 'custom_add_login_logout_link', 10, 2 );

In this example we are targeting primary menu. If you have not registered menu then you have to register nav menu with register_nav_menus function.

<?php
register_nav_menus( array(
    'primary' => __( 'Primary Menu' ),  
 ) );

One thought on “How to add extra menu item in WordPress Navigation menu

Leave a Reply

Your email address will not be published. Required fields are marked *