Snippet

How to Redirect Users After Logout in WordPress

How to redirect after logout in wordpressWordpress logout redirect to homepagePlugin for redirect after logout wordpressSet custom url for logout redirect wordpressWordpress redirect users after logoutRedirect to specific page after logout wordpressWordpress logout redirect pluginChange logout redirect url wordpressWordpress logout redirect settingsRedirect 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

1add_action( 'wp_logout', 'wp_dudecom_redirect_after_logout' ); 2 3function wp_dudecom_redirect_after_logout() { 4 // Set the URL to redirect to after logout 5 $redirect_url = home_url(); 6 7 // Redirect to the specified URL 8 wp_safe_redirect( $redirect_url ); 9 exit(); 10}

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.