Create a Login/Logout Link in WordPress

To create a login/logout link in WordPress you can use a snippet of php code using the wp_logout_url function, you can also set the logout URL to be an external site to your own.

<?php if (is_user_logged_in()) : ?>
    <a href="<?php echo wp_logout_url(get_permalink()); ?>">Logout</a>
<?php else : ?>
    <a href="<?php echo wp_login_url(get_permalink()); ?>">Login</a>
<?php endif;?>

If you just want a logout link only:

<?php if (is_user_logged_in()) : ?>
    <a href="<?php echo wp_logout_url(get_permalink()); ?>">Logout</a>
<?php endif;?>

If you just want a login link only:

<?php if (! is_user_logged_in()) : ?>
    <a href="<?php echo wp_logout_url(get_permalink()); ?>">Login</a>
<?php endif;?>

You can add this anywhere in your theme template files – if you want to add as a widget – you need to enable PHP for widgets.

To set the link when logging out to some external URL you can change what gets passed into the wp_logout_url() function, for example…

wp_logout_url( 'http://somewhereelse.com' );

But before it will work you need to add it to a list of allowed URLs, so in your functions.php file add…

add_filter('allowed_redirect_hosts','allow_ms_parent_redirect');
function allow_ms_parent_redirect($allowed) {
 $allowed[] = 'somewhereelse.com';
 return $allowed;
}

Now the logout will pass you onto another site.

To add the login/logout as a shortcode, you can add the below in functions.php and the use the [login_logout] shortcode.

ref

Here’s how you can add a login/logout link to an existing menu.