Redirecting users to a custom URL after logout in WordPress is a simple and effective way to improve user experience. Whether you want users to land on a homepage, a thank-you page, or a feedback form, you can achieve this with a few lines of code. Here’s how you can do it:
- Add the Code to Your Theme’s
functions.php
File:
First, open yourfunctions.php
file located in your active theme directory. Then, add the following code snippet:
function custom_logout_redirect() {
wp_redirect('custom_url'); // Replace 'custom_url' with your desired URL
exit();
}
add_action('wp_logout', 'custom_logout_redirect');
2. Customize the Redirect URL:
Replace 'custom_url'
with the URL you want users to be redirected to. For instance, if you want them to go to the homepage, you can use:
wp_redirect(home_url());
Or, for a specific page, use:
wp_redirect('https://yourdomain.com/thank-you');
3. Add a Logout Link:
Ensure you have a logout link on your site where users can easily find it. You can add a logout button using this code:
<a href="<?php echo wp_logout_url(); ?>">Logout</a>
This code snippet will ensure that users are redirected to the specified URL whenever they log out. It’s a simple yet effective way to guide users to the pages you want them to see after they end their session.
Remember, the logout URL in WordPress is dynamic, which means it includes a unique security token called a nonce. By using wp_logout_url()
, WordPress handles this for you, ensuring secure and reliable redirects.
Give it a try on your WordPress site and see how it enhances your user experience!