
This is an easy guide on how to redirect users to the homepage without a plugin after they have clicked the Logout link or button in their WordPress profile or custom account page.
Problem: Redirect to homepage after logout in WordPress
If you are using WordPress plugins like BuddyPress, WooCommerce, Ultimate Member, LearnDash, Easy Digital Downloads, Members, Membership Pro, bbPress, etc. that allow users to register or create an account, then you might want to avoid redirecting them to the default WordPress Login page either for security reasons or because you have a custom login page visible at the home page.
Solution: How to redirect to homepage after Logging out without plugin
To automatically redirect users to your WordPress website’s home page after they have logged out, then copy and paste the code below at the end of your WordPress theme functions.php file.
//redirect users to homepage after logging out
add_action('wp_logout','redirect_to_homepage_after_logout');
function redirect_to_homepage_after_logout(){
wp_safe_redirect( home_url() );
exit;
}
How the code works
Once you click the Logout link, the logout action will be performed through wp_logout function.
home_url is used to retrieve your home page or front-page URL for instance, http://example.com where the user will be redirected to.
Let us know whether this code worked for you. If it did not work, let us know whether the problem is related to a specific WordPress plugin in the comment section below. We will do our best to find a solution to your problem for free.
Leave a Reply