How to Redirect Users After Logout in WordPress

How to redirect after logout in wordpress; Wordpress logout redirect to homepage; Plugin for redirect after logout wordpress; Set custom url for logout redirect wordpress; Wordpress redirect users after logout; Redirect to specific page after logout wordpress; Wordpress logout redirect plugin; Change logout redirect url wordpress; Wordpress logout redirect settings; Redirect users to homepage after logout wordpress;

Explanation

Want to send users to a specific page after they log out of your WordPress site? This little snippet does just that!

Here's how it works:

  • When someone logs out, WordPress triggers an event called wp_logout.
  • We've hooked into this event with a custom function named wp_dudecom_redirect_after_logout.
  • Inside this function, we set a $redirect_url to the homepage using home_url(). You can change this to any URL you prefer.
  • Then, we use wp_safe_redirect() to send the user to the URL we specified.
  • Finally, exit() ensures the script stops running after the redirect.

Tip: If you want to redirect to a different page, just replace home_url() with the URL of your choice, like 'https://yourwebsite.com/your-page'.

Code

add_action( 'wp_logout', 'wp_dudecom_redirect_after_logout' );

function wp_dudecom_redirect_after_logout() {
    // Set the URL to redirect to after logout
    $redirect_url = home_url();
    
    // Redirect to the specified URL
    wp_safe_redirect( $redirect_url );
    exit();
}

Instructions

File Location: Add the following code to your theme's functions.php file or a custom plugin file.

Prerequisites: None required.

Implementation Steps:

  1. Access your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor if you are editing the functions.php file, or go to Plugins > Editor if you are using a custom plugin.
  3. Locate and open the functions.php file or your custom plugin file.
  4. Copy and paste the provided code snippet into the file:
  5. 
        add_action( 'wp_logout', 'wp_dudecom_redirect_after_logout' );
    
        function wp_dudecom_redirect_after_logout() {
            // Set the URL to redirect to after logout
            $redirect_url = home_url();
            
            // Redirect to the specified URL
            wp_safe_redirect( $redirect_url );
            exit();
        }
        
  6. Save the changes to the file.
  7. Test the logout functionality by logging out of your WordPress site to ensure the redirect works as expected.

Note: To redirect to a different page, modify $redirect_url = home_url(); to your desired URL, such as $redirect_url = 'https://yourwebsite.com/your-page';

If you need assistance with this implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert WordPress support.