How to redirect BuddyPress user after login

WordPress Snippets

To redirect only BuddyPress users after login, you can use the wp_login action hook in your theme’s functions.php file or in a separate plugin file. This hook is called after a user has successfully logged in.

You can use the bp_is_active function to check if the BuddyPress plugin is active, and the bp_loggedin_user_domain function to get the logged-in user’s BuddyPress profile URL.

Here’s an example of how you can use these functions to redirect only BuddyPress users after login:

// Redirect BuddyPress user after login
// More snippets at wpunplugged.com

function my_custom_login_redirect( $user_login, $user ) {
	if ( bp_is_active( 'xprofile' ) ) {
		// Redirect the user to their BuddyPress profile URL
		wp_redirect( bp_loggedin_user_domain() );
		exit;
	}
}
add_action( 'wp_login', 'my_custom_login_redirect', 10, 2 );

This code will redirect BuddyPress users to their profile URL after they have successfully logged in. Non-BuddyPress users will not be redirected.

You can also use the bp_core_login_redirect filter to modify the default login redirect URL for BuddyPress. Here’s an example of how you can use this filter to redirect only BuddyPress users:

// Default login redirect BuddyPress user login
// More snippets at wpunplugged.com

function my_custom_login_redirect_url( $redirect_to_calculated, $redirect_url_specified, $user ) {
	if ( bp_is_active( 'xprofile' ) ) {
		// Redirect the user to their BuddyPress profile URL
		return bp_loggedin_user_domain();
	}

	// Return the default redirect URL for non-BuddyPress users
	return $redirect_to_calculated;
}
add_filter( 'bp_core_login_redirect', 'my_custom_login_redirect_url', 10, 3 );

This code will redirect BuddyPress users to their profile URL after they have successfully logged in. Non-BuddyPress users will be redirected to the default redirect URL.

Share this post

You might also like...

MemberPress Membership login for WordPress Review
Content restriction

MemberPress for WordPress Review

MemberPress for WordPress is a powerful membership plugin that makes it easy to create and manage memberships on any WordPress website. It allows you to

Read More »

Comment

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