How to log out a user programmatically in WordPress

In this article, we shall delve into the methods of logging out users programmatically in WordPress. We will analyze various methods that will help you achieve this task with ease, from using a convenient plugin to developing customized code tailored for your needs.

User authentication in WordPress

WordPress manages user sessions using session data and cookies. When a user logs in to a WordPress website, WordPress creates a session for that user, which stores information about the user, including their login status and other relevant data. When the user logs out, WordPress destroys the session and all associated session data and cookies.

The intricacies of logging out users programmatically

To programmatically log out a user from WordPress, one must create a custom function that destroys the user’s session, along with all its associated data and cookies.

wordpress logout user programmatically

This process involves utilizing the WordPress Core functions, such as calling the wp_logout()  function to remove all authentication cookies and related data. In addition, the wp_redirect()  function can redirect the user to a designated page, such as the login page. Detailed documentation on these and more functions can be found in the WordPress Codex. Armed with this knowledge, logging out of WordPress programmatically can be easily accomplished.

Here’s how to do it:

Step 1: Creating a custom function for logging out users

To create a custom function in WordPress, we’ll need to use PHP code. Here’s an example of what the code might look like:

function custom_logout_current_user() {

Let’s break down what this code does. The wp_logout() function logs the user out of WordPress by destroying their session and all associated data and cookies. The wp_redirect() function fires after a user is logged out and redirect the user to the homepage of the website. Finally, the exit statement stops the script from executing any further.

You can customize this function to fit your specific needs. For example, you might want to redirect the user to a different page after they programmatically logout, or you might want to add some additional functionality.

Step 2: Integrating the custom function into a WordPress website

Now that we have our custom function, we need to add it to our WordPress website. There are a few different ways to do this, but one common approach is to add the function to your WordPress theme or plugin.

To add the function to your theme, open the functions.php file in your theme directory and add the following code:

add_action('init', 'custom_logout_function');

This code will run the custom_logout_function function on the init hook, which is a WordPress action that runs when WordPress initializes.

How to force logout in WordPress with a plugin

If you are hesitant to add custom code to your WordPress site, there is an alternative approach you can take. By using a plugin like “WP Force Logout” you can force the logout of all or selected users with a single click.

WPForce Logout plugin

WPForce Logout is a WordPress plugin that allows administrators to log out all or selected users with one click. This can be advantageous for safeguarding user accounts from brute force attacks or in the event of a site compromise. The plugin can also be used to force all users to log out when working on a site or when dealing with a potential security issue. In addition, the plugin offers an easy way to view online and offline users and their last login activity directly from the user tab. No additional settings are required, simply install and activate the plugin.

Here are the steps to install and use the plugin:

  1. Go to your WordPress Dashboard and navigate to “Plugins” -> “Add New”.
  2. Search for “WP Force Logout” in the search bar and click “Install Now”.
  3. Once the plugin is installed, click “Activate”.
  4. Go to “Settings” -> “WP Force Logout” to configure the plugin settings.
  5. Now from the WordPress users list you can choose to force logout for a specific user or all users.

That’s it! The plugin will now force the logout of all selected users.

Note that this plugin can be a powerful tool, so use it with caution. Make sure you understand the impact of forcing logout on your users before using the plugin.


In this tutorial, we’ve shown you how to log out a user programmatically in WordPress, using a custom function and dedicated WordPress plugin. By implementing a logout feature, you can help protect the security and privacy of your users and ensure that their data and content are kept safe.