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

Backup

Jetpack for WordPress Review

Jetpack for WordPress is an amazing plugin that offers a wide range of features to help make your WordPress website more secure, efficient and user-friendly.

Read More »

Comment

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