BuddyPress: How to Redirect Upon Logout

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

7 Comments


  1. 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.

    Reply

  2. Wow, this code worked superbly within the seconds when applied to my BP site.

    Thanks and keep posting such recipes.

    Seth

    Reply

  3. 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.

    Reply

    1. 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/

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *