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