Automatically add logout login link to your Wordpress navigation menu

There is a really simple piece of PHP code that you have to drop in the functions.php file in your currently activated theme. And with this only, it will add a new list item in the menu that will show Logout URL if user is logged in, else a Login URL is shown.

[php]
add_filter( ‘wp_nav_menu_items’, ‘add_loginout_link’, 10, 2 );

function add_loginout_link( $items, $args ) {

$menu_location = ‘_THEME_LOCATION’;

if (is_user_logged_in() && $args->theme_location == $menu_location ) {

$items .= ‘<li><a href=”‘. wp_logout_url() .'”>Log Out</a></li>’;

}

elseif (!is_user_logged_in() && $args->theme_location == $menu_location) {

$items .= ‘<li><a href=”‘. site_url(‘wp-login.php’) .'”>LogIn</a></li>’;

}

return $items;

}

 

[/php]

 

Please replace _THEME_LOCATION with the location of your menu.

Thanks

There is a really simple piece of PHP code that you have to drop in the functions.php file in your currently activated theme. And with this only, it will add a new list item in the menu that will show Logout URL if user is logged in, else a Login URL is shown.

[php]
add_filter( ‘wp_nav_menu_items’, ‘add_loginout_link’, 10, 2 );

function add_loginout_link( $items, $args ) {

$menu_location = ‘_THEME_LOCATION’;

if (is_user_logged_in() && $args->theme_location == $menu_location ) {

$items .= ‘<li><a href=”‘. wp_logout_url() .'”>Log Out</a></li>’;

}

elseif (!is_user_logged_in() && $args->theme_location == $menu_location) {

$items .= ‘<li><a href=”‘. site_url(‘wp-login.php’) .'”>LogIn</a></li>’;

}

return $items;

}

 

[/php]

 

Please replace _THEME_LOCATION with the location of your menu.

Thanks