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.
Here’s how you can add a login/logout link to an existing menu.
15 comments
Jesse
I copied and pasted the long PHP code in my functions.php but it could not be saved because the system found something wrong with the code. What had gone wrong? Any help?
Alyssa
Worked like a charm. Thanks! Although the shortcode is [login_logut] not “logout”. But to guys reading this, you can change
add_shortcode( ‘login_logut’, ‘login_logut’ );
into
add_shortcode( ‘login_logout’, ‘login_logout’ );
and then change
function login_logut()
into
function login_logout()
but if you don’t want to mess with the code just use [login_logut] as shortcode
Neil Gowran
Thanks for picking that out – I have updated the code to spell a bit more logically
kostas
thank you for sharing
Amanda
Thank you for the above. I am using the shortcode for a dynamic login/logout link. How can I modify the php code so admin is redirected to the admin dashboard upon login and subscribers are redirected to the homepage? Thank you in advance for any help.
Tĩnh
Hi – Thanks for this information. But
I want to add to the header-icon-menu of the outfitter theme.
Please help me!
kunal
Thanks! you solved my problem
Bill G
This is pretty vague… “anywhere in your Theme Template Files”
Which file? under which directory?
Neil Gowran
If you find that vague, best you learn some basics first.
Gabi
Hi
How do I add the logout link to the primary menu only?
Jennifer
Hi – Thanks for this information. Works great:) Can you tell me how I would customize the logout text?
Jen
Neil Gowran
You can use the loginout filter – https://core.trac.wordpress.org/ticket/34356
Richard
What if you want to redirect to a “logged out” page, after the logout command has run ?
john
thank u , it woks fine
Christian Schäfer
Worked fine! Thanks for the snippet – made my day :)