Many times in the past, I have found myself logging into my blog via the Log in link in my sidebar when I want to update a post. Upon logging in, WordPress would send me to the dashboard. Instead, I would have preferred for WordPress to keep me on the same page I logged in from. To fix this, you can use WordPress’s functions to set up your theme’s login and log out links to send the user wherever you’d like.

The simple, classic way of handling login / log out links in WordPress is to place the following code where you want the link to appear:

This generates a “Log in” link when the user is logged out, and a “Logout” link when the user is logged in. The “Log in” link redirects to the dashboard once the user has inputted their username and password successfully, and the “Log out” link redirects to the Login screen.

Redirect login / log out

The wp_loginout() function accepts a $redirect parameter that can send the user to a location other than the defaults.

To send the user to a specific page upon logging in or out, specify a redirect:

The above code sends the user to your site (site_url() for home, or use site_url(‘/mypage’) for a page of your site) after they’ve been logged in or out. You can also use an absolute URL in quotes. You may even use get_permalink() to send them to the “current” page they logged in or out from.

Redirect login and log out separately

You can also set different redirects for your login and log out links using the is_user_logged_in() function along with wp_login_url() and wp_logout_url().

is_user_logged_in() is a boolean that returns a true/false value. Therefore, we can use it to hide or display login / log out / admin links. Here is an example of how this is used:

<?php if (is_user_logged_in()) { ?>
	<!-- User is logged in: show Admin and Log out link -->
<?php } else { ?>
	<!-- User is logged out: show Log in link -->
<?php } ?>

To use is_user_logged_in() to show an admin and log out link to a user if they are logged in and a login link only if the user is not logged in:

The log in redirects back to the current page, whereas the log out link redirects to the home page. This is the method I personally prefer. As shown above, this link can be a WordPress tag or it can be an absolute URI (including http://). Relative URIs will not work.