Try this code in your functions.php file –
add_filter( 'wp_nav_menu_items', 'sp_add_loginout_link', 10, 2 );
function sp_add_loginout_link( $items, $args ) {
// Change 'primary' to 'secondary' to put the login link in your secondary nav bar
if ( $args->theme_location != 'primary' )
return $items;
if ( is_user_logged_in() ) {
$items .= '<li class="menu-item"><a href="'. wp_logout_url() .'">Log Out</a></li>';
} else {
$items .= '<li class="menu-item"><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
}
return $items;
}
This works for the Primary Menu so you’ll have to make adjustments to the code in order for it to work with the Secondary Menu.
Source – https://gist.github.com/nickcernis/02086c75ee9aa19447e0
I tested on a regular Primary Menu and it worked perfectly for “Logged in” and “Out” users. You can test to see if your adjustments work for the Secondary Menus as well.
Currently, I’m not using any Anti Spam plugins so I’m not sure which one would be best at the moment. I’d check the reviews to see what other users are prefer.