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( '' );

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

function allow_ms_parent_redirect($allowed) {
 $allowed[] = '';
 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.

<?php //<~ remove if using in functions.php
add_shortcode( 'login_logut', 'login_logut' );
* Add a login/logout shortcode button
* @since 1.0.0
function login_logut() {
if (is_user_logged_in()) :
// Set the logout URL - below it is set to the root URL
<a role="button" href="<?php echo wp_logout_url('/'); ?>">Log Out</a>
else :
// Set the login URL - below it is set to get_permalink() - you can set that to whatever URL eg '/whatever'
<a role="button" href="<?php echo wp_login_url(get_permalink()); ?>">Log In</span></a>
return ob_get_clean();
view raw login-out.php hosted with ❤ by GitHub



  1. kunal on September 8, 2017 at 6:53 am

    Thanks! you solved my problem

  2. Bill G on January 19, 2017 at 9:23 pm

    This is pretty vague… “anywhere in your Theme Template Files”

    Which file? under which directory?

  3. Gabi on October 12, 2016 at 4:52 pm

    How do I add the logout link to the primary menu only?

  4. Jennifer on August 15, 2016 at 5:27 pm

    Hi – Thanks for this information. Works great:) Can you tell me how I would customize the logout text?


  5. Richard on July 28, 2016 at 12:57 pm

    What if you want to redirect to a “logged out” page, after the logout command has run ?

  6. john on August 27, 2015 at 12:34 pm

    thank u , it woks fine

  7. Christian Schäfer on May 13, 2015 at 9:45 am

    Worked fine! Thanks for the snippet – made my day :)

Leave a Comment