How to create a login/logout link in WordPress using wp_loginout

The WordPress wp_loginout function is a part of the WordPress core that provides a specific functionality related to user login and logout. This function displays either the ‘Log in’ or ‘Log out’ link depending on the user’s current status. If the user is already logged in, it will show the ‘Log out’ link, and if the user is not logged in, it will show the ‘Log in’ link.

It serves a dual purpose. Firstly, it provides a direct link for users to log in to the site. Secondly, it offers a straightforward way for logged in users to log out. This dual functionality makes it a fundamental part of user management in a WordPress site.

Moreover, the wp_loginout function can be customized to redirect users to a specific page after logging in or logging out. This can be beneficial for directing users to a particular page depending on their actions, improving the user experience and site navigation.

Parameters Accepted by the WordPress wp_loginout Function

The wp_loginout function in WordPress accepts two parameters. These parameters are:

  • $redirect (string): This is an optional parameter. By default, it is set to an empty string (”). It is used to specify an optional path to which the user will be redirected upon successful login or logout.
  • $display (bool): This parameter is also optional and its default value is true. It determines whether the login/logout link will be echoed (displayed) or returned. If set to true, the link will be echoed. If set to false, the link will be returned.

Returned Value of the wp_loginout Function

The value returned by the wp_loginout function depends on the $display parameter. If $display is set to true, the function will not return a value (void). On the other hand, if $display is set to false, the function will return the login/logout link as a string.

Examples

Example 1: How to Display a Login/Logout Link in WordPress

This is the most basic usage of the wp_loginout() function. It will display a login link if the user is not logged in, and a logout link if the user is logged in.

<?php
if (!is_user_logged_in()) {
 wp_loginout(home_url()); // Display 'Log in' link.
} else {
 wp_loginout(home_url()); // Display 'Log out' link.
}
?>

Example 2: How to Customize the Redirect URL After Login/Logout in WordPress

In this example, the wp_loginout() function is used to redirect the user to a specific page after they login or logout. The URL of the page is passed as an argument to the function.

<?php
if (!is_user_logged_in()) {
 wp_loginout('http://www.yoursite.com/welcome/'); // Redirect to 'welcome' page after login.
} else {
 wp_loginout('http://www.yoursite.com/goodbye/'); // Redirect to 'goodbye' page after logout.
}
?>

Conclusion

The wp_loginout function in WordPress is a highly practical tool that aids in the generation of login or logout URLs. This function is beneficial for developers who need to provide users with an easy and efficient method of logging in or out of a WordPress site. By automatically detecting the user’s login status and changing the URL accordingly, it simplifies the process for both the developer and the user. Therefore, the wp_loginout function is a valuable asset in the WordPress function library for creating user-friendly interfaces and enhancing site navigation.

Related WordPress Functions