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.
Excellent post. I was checking continuously this blog and I’m
impressed! Extremely helpful info specially the last part 🙂 I care for such info a lot.
I was seeking this certain information for a long time.
Thank you and good luck.
An impressive share! I have just forwarded this onto a friend who had been conducting a little research on this.
And he actually bought me dinner due to the fact that I discovered it for him…
lol. So allow me to reword this…. Thanks for the
meal!! But yeah, thanks for spending some time
to discuss this topic here on your site.
You really make it seem so easy with your presentation but I
find this matter to be actually something that I think I would never understand.
It seems too complicated and extremely broad for me. I’m looking
forward for your next post, I will try to get the
hang of it!
Wow! This blog looks just like my old one!
It’s on a entirely different topic but it has pretty much the same page
layout and design. Superb choice of colors!
Attractive portion of content. I just stumbled upon your web site and in accession capital to say that I get actually enjoyed account your
blog posts. Any way I’ll be subscribing to your feeds or
even I achievement you get admission to persistently rapidly.
I think this is among the such a lot vital info for me.
And i am satisfied studying your article. But want to remark on few general things, The web site taste is great, the articles is
actually excellent : D. Just right task, cheers
Thanks for some other wonderful article. The place
else may just anybody get that kind of info in such an ideal method of writing?
I’ve a presentation next week, and I’m at the look for such info.
I love it when folks come together and share ideas.
Great site, stick with it!