Note from 2022: Take a peek in the comment section before trying this code!
So, you’ve setup a custom menu in BuddyPress that allows users to logout at the click of a button. But once they’ve logged out, chaos ensues. I know. I’ve been there. Wouldn’t it be nice if your users could just redirect upon logout to your homepage?
They can. Here’s how.
- Step 1: Back-up your website.
- Step 2: Make sure you are using a child theme.
(You are using a child theme, right? If not, stop everything and go here to learn how to set one up.)
- Step 3: Go to your WordPress Dashboard and navigate to:
- Appearance > Themes > Editor
- Step 4: Select the file called “functions.php” in your child theme.
- Step 5: Copy and paste the following code under any other customizations you’ve entered:
//* Redirect WordPress to Homepage Upon Logout
add_action('wp_logout',create_function('','wp_redirect(home_url());exit();'));
- Step 5: Save your changes and then take it for a test drive.
Viola! Now your users can happily redirect upon logout! This tip works for BuddyPress as well as WordPress.
Did it work? Trying to do something similar but not sure where to start? Let me know in the comments below.
Extra:
Now perhaps you want this feature to stick around even when you change themes. In that case, take this same code snippet and turn it into a “Must Use Plugin.” Never made one before? I’ll cover that in an upcoming blog post.
Subscribe here and I’ll let you know when I post new tutorials and code snippets:
Opt-In
Hey there! I just would like to offer you a big thumbs up for the great info you have right
here on this post. I’ll be coming back to your site for more soon.
Informative article, exactly what I needed.
Wow, this code worked superbly within the seconds when applied to my BP site.
Thanks and keep posting such recipes.
Seth
Hi, Guys
Checkout a new Free BuddyPress add-on plugin.
https://wbcomdesigns.com/downloads/buddypress-redirect/
BuddyPress Redirect plugin serves the purpose of redirecting to different locations according to the user role after login.
Tested with WordPress 4.9.6 and BuddyPress 3.1.0
BuddyPress Redirect plugin gives an option to BuddyPress based website owners to decide that where their users should be redirected after the login. Currently, users can be redirected to 3 different locations after login.
1. Personal Profile
2. Personal Activity
3. Site Wide Activity
4. Custom URL
BuddyPress Redirect plugin also offers options for logout redirect, user can be redirected to custom url after logout.
Hi – thanks for this snippet
however you might want to consider updating it to a more ‘safe’ method using wp_safe_redirect
see https://developer.wordpress.org/reference/functions/wp_redirect/
Todd, that is an excellent point! I could always try to argue that it would be overkill in this scenario since I “know” what value home_url will return, but that’s not your point. Sanitizing all data prior to using it is a good habit to get into it, especially for new developers who aren’t stuck in their ways like me. 🙂
I’ll play around with this when I have more time to post an update:
https://developer.wordpress.org/reference/functions/wp_safe_redirect/
Worked perfectly, thanks!
Thanks! Just a little update if someone, like me, finds this:
// Redirect WordPress to Homepage Upon Logout
add_action(‘wp_logout’, function(){
wp_safe_redirect(home_url());
exit;
});
You might want to make the changes mentioned above as this snippet broke my site: Function create_function() is deprecated
Worked perfect… I have been looking for a solution to this for what seems an eternity. Thank you.
Will definitely subscribe to your site.