How to create a “Logout” button in WordPress?

The following is some PHP code to add a Login and Logout link to a Wordpress Theme. Or in other words, toggling the logout/login button in Wordpress.   Making it so that logged in site visitors see a logout button and not logged in visitors see a login button.


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

Explaining the Code

The code above uses the is_user_logged_in function to determine if the visitor to your Wordpress is logged in or not.  Displaying a link based on the login/logout status of the end user.  It uses wp_login_url to display the proper WordPress Login URL and wp_logout_url to display the proper Wordpress Logout URL.

This tip works for WordPress 2.7 and greater

Tags: ,

3 Responses to “How to create a “Logout” button in WordPress?”

  1. Dave says:

    Thank you for being straight forward with how to do this. I am a novice when it comes to wordpress and this helps!!!!

  2. Scott KH says:

    Where do I place this php code?

  3. admin says:

    In your custom theme, or the them you’re editing to add the feature. If you have a header.php file in your theme it would probably go there. Really just depends where you want the login/logout button to show up.

Leave a Reply