By default, after you log out from the WordPress dashboard, you will be redirected to the WordPress default login page (wp-login.php). The WordPress login page (wp-login.php) is the default login page for all roles of WordPress users, from the user role with the most capabilities (Administrator) to the least (Subscriber).
With your wp-login page which is a gateway for your site still the default page when you log out, it may encourage people with ill intention to harm your website by trying a brute force attack to get in as users with high capabilities so they can mess up your website from inside.
For the above reasons, you may want to create a custom login page or you can place a login form on your homepage so you can usher the standard user to log in from there. If you are using Elementor as the page builder for your site, we already covered how to create a custom login page with Elementor in our previous article.
And how about the default log-out page, are the users still being redirected to the wp-login page when they log out?
Unfortunately, yes. But no worries, in this article we will show you how to redirect users to the homepage and custom URL after they log out from your site.
Methods to Redirect Users After Logout
Now that you know why redirecting users after they log out is important, there are two ways to do it:
- Using a WordPress plugin
- Using custom code
Method 1: Redirect Users Using a WordPress Plugin
In WordPress, you can solve almost any kind of problem and need by using plugins. This time we choose the LoginWP by LoginWP Team for our tutorial.
The free version of the LoginWP plugin allows you to redirect users to the homepage or any page and set the redirection condition by the user role, username, or capabilities you want when they log out, which is exactly what we need for this time. And the Pro version of the plugin includes all functions of the free version with addition you can redirect users to the current page they are logging in from or back to the previous (referrer) page after login, and integrations with top plugins like WooCommerce, WPFroms, LearDash, and many more.
Go ahead to install and activate the plugin to proceed to the next step, and once you are ready let’s get into the matter.
Redirect Users to Homepage or Custom URL
To redirect users to the homepage or custom URL, from your WordPress dashboard, start by going to the LoginWP → Redirections, and continue by adding a new rule by clicking the Add New button.
The next step is selecting the Rule Condition, you can choose who you want to set the redirect rule for, and for this tutorial, we will select the subscriber role which has the least capabilities from the user role.
Move on to the Redirect URLs section. Here you can set the URL to redirect the users after they log in to the website in the Login URL field and after they log out in the Logout URL field. And for the URL you can set your homepage URL to redirect the users to your homepage, or, you can set a custom URL if you want to.
As you can see in the image above, we set the Login URL and Logout URL to our homepage URL to redirect users to the homepage after they log in or log out.
And once the rules and URLs are set, activate the redirect rule by clicking on the Save Rule button.
Method 2: Redirect Users Using a Custom Code
For the second method, we will show you how to redirect the user to the homepage after they log out without using a plugin. Instead, you will use a custom code to achieve that task.
Before you start, you may want to backup your site and use a child theme to avoid breaking the parent theme or losing your changes when you update your theme.
Now for the steps, start from your WordPress dashboard, then go to Appearance → Theme File Editor. Once you’ve arrived on the Edit Themes page, select the functions.php file from the Theme Files list by clicking it. You can place the custom code in your functions.php file under the PHP tag <?php
and outside of any existing function.
And now for the code, there are two custom codes we will share with you here, the first one is to redirect the user to the homepage. The code is as follows:
- Redirect the user to the homepage
add_action('wp_logout','auto_redirect_after_logout'); function auto_redirect_after_logout(){ wp_safe_redirect( home_url() ); exit(); }
And the second code is to redirect the user to a custom URL. To use the code for redirecting the users to the page you want after they are logged out, you need to replace the https://www.yourcustomurl.com
URL with your URL. Here is the code:
- Redirect the user to a custom URL
add_action('wp_logout','auto_redirect_external_after_logout'); function auto_redirect_external_after_logout(){ wp_redirect( 'https://www.yourcustomurl.com' ); exit(); }
Once you have the code in its place, save your changes by clicking on the Update File button.
The Bottom Line
Besides its design and functionality, security is one of the aspects you need to notice when you have a WordPress site. To keep your site secure, you need to update the WordPress core software, plugins, and theme, then add a security plugin to add more protection to your site.
On a daily basis, some people with ill intentions with a whole bunch of malicious robots can try to break in. One of the methods they may try is the brute force attack that targets your login page. Changing your login or logout redirect is one of the methods you may want to try to keep your login page safe and additionally helps you to take the user to the page you want them to be.
4 thoughts on “How to Redirect Users to Homepage After Logout in WordPress (with and without Plugin)”
I tried this using the Astra theme and it made no difference unfortunately. I saved the update to the functions.php file in WordPress and then tested it with private log in and it still goes to the WP login page even with the snippet above in place. Any ideas where I’m going wrong? I’m on WP 8.1 using Astra as I mentioned.
Thanks
I could not refrain from commenting. Exceptionally well
written!
I was able to find good advice from your content.
Thanks for finally talking about How to Redirect Users to Homepage After Logout in WordPress