• Replacing the code in the original bp-login-redirect.php with the code below extends the plugin by another option:

    It lets you redirect users to ‘their group’ after login. This is especially useful if users only have one group. If users are in several groups it will send them to the group with the lowest ID (usually oldest group). Admministrators and users who are not in any groups fall back to Custom URL instead. It’s easy to add additional exemptions.

    It’s also possible to use this to send users of one role to a custom URL. Just change ‘administrator’ in the code to the role you are aiming at and fill in the Custom URL field in the plugin setting.

    <?php
    /*
    Plugin Name: BP Login Redirect
    Description: allows the buddypress site admins to decide where to redirect their users after login. Now logout redirection is set to Homepage.
    Contributors: j_p_s
    Author: Jatinder Pal Singh
    Author URI: https://www.jpsays.com
    Version: 2.2
    Stable Tag: 2.2
    Tested up t0: 4.1
    
    */
    ?>
    <?php
    add_action( 'admin_menu', 'blr_add_admin_menu' );
    add_action( 'admin_init', 'blr_settings_init' );
    add_filter("login_redirect","bp_login_redirection",100,4);
    add_action('wp_logout','blr_logout_redirect');
    
    function blr_add_admin_menu(  ) { 
    
    	add_options_page( 'BP Login Redirection', 'BP Login Redirection', 'manage_options', 'bp_login_redirection', 'bp_login_redirection_options_page' );
    
    }
    
    function blr_settings_init(  ) { 
    
    	register_setting( 'pluginPage', 'blr_settings' );
    
    	add_settings_section(
    		'blr_pluginPage_section',
    		__( '<a href="https://www.jpsays.com" alt="www.jpsays.com">By Jatinder Pal Singh</a>', 'wordpress' ),
    		'blr_settings_section_callback',
    		'pluginPage'
    	);
    
    	add_settings_field(
    		'blr_radio_field_0',
    		__( 'Redirection Type', 'wordpress' ),
    		'blr_radio_field_0_render',
    		'pluginPage',
    		'blr_pluginPage_section'
    	);
    
    	add_settings_field(
    		'blr_text_field_1',
    		__( 'custom url', 'wordpress' ),
    		'blr_text_field_1_render',
    		'pluginPage',
    		'blr_pluginPage_section'
    	);
    
    }
    
    function blr_radio_field_0_render(  ) { 
    
    	$options = get_option( 'blr_settings' );
    	?>
    	<table>
    	<tr><td>Personal Profile / Personal Activity:</td><td><input type='radio' name='blr_settings[blr_radio_field_0]' <?php checked( 1,$options['blr_radio_field_0'], true ); ?> value='1'></td></tr>
    	<tr><td>Site Wide Activity:</td><td><input type='radio' name='blr_settings[blr_radio_field_0]' <?php checked( 2,$options['blr_radio_field_0'], true ); ?> value='2'></td></tr>
    	<tr><td>Redirect to a group of which user is a member </td><td><input type='radio' name='blr_settings[blr_radio_field_0]' <?php checked( 3,$options['blr_radio_field_0'], true); ?> value='3'></td></tr>
    	<tr><td>Custom URL:</td><td><input type='radio' name='blr_settings[blr_radio_field_0]' <?php checked( 4,$options['blr_radio_field_0'], true); ?> value='4'></td></tr>
    	<tr><td>Friends' Activity:</td><td><input type='radio' name='blr_settings[blr_radio_field_0]' <?php checked( 5,$options['blr_radio_field_0'], true); ?> value='5'></td></tr>
    	</table>
    	<?php
    
    }
    
    function blr_text_field_1_render(  ) { 
    
    	$options = get_option( 'blr_settings' );
    	?>
    	<input type='text' name='blr_settings[blr_text_field_1]' value='<?php echo $options['blr_text_field_1']; ?>'>
    	<?php
    
    }
    
    function blr_settings_section_callback(  ) { 
    
    	echo __( '<b><u>Note:</u></b> <i>Custom URL</i> will not work with any external link. Custom URL must be from your wordpress site only<b><br>(if user is not member of any group <br>this falls back on <i>Custom URL</i>', 'wordpress' );
    
    }
    
    function bp_login_redirection_options_page(  ) { 
    
    	?>
    	<form action='options.php' method='post'>
    
    		<h2>BP Login Redirection</h2>
    
    		<?php
    		settings_fields( 'pluginPage' );
    		do_settings_sections( 'pluginPage' );
    		submit_button();
    		?>
    
    	</form>
    	<?php
    
    }
    function bp_login_redirection($redirect_url,$request_url,$user)
    {
    	global $bp;
    	$options = get_option( 'blr_settings' );
    	$selected_option = $options['blr_radio_field_0'];
    	if($selected_option == '1')
    	{
    		return bp_core_get_user_domain($user->ID);
    	}
    	elseif($selected_option=='2')
    	{
    		$activity_slug = bp_get_activity_root_slug();
    		$redirect_url = $bp->root_domain."/".$activity_slug;
    		return $redirect_url;
    	}
    	elseif($selected_option=='3')
    		{
    							//$activity_slug = bp_get_activity_root_slug();
    							//$redirect_url = $bp->root_domain."/".$activity_slug;
    
    			if ( bp_has_groups()  )
    				{
    					if (bp_get_total_group_count_for_user($user->ID) < 1 ||  in_array( 'administrator', (array) $user->roles )   )
    						{
    
    				$redirect_url = $options['blr_text_field_1'];
    		return $redirect_url;
    						}
    
    					 while ( bp_groups() )
    					 	{
    							bp_the_group(); 
    
    						 	if (groups_is_user_member( $user->ID, bp_get_group_id() ))
    						 		{
    						 			$redirect_url = bp_get_group_permalink();
    
    						 			return $redirect_url;break;
    						 		 }
    
    						        // do_action( 'bp_directory_groups_actions' );
    					 	}
    
    					 // do_action( 'bp_after_groups_loop' );
    				}
    
    		}
    	elseif($selected_option=='4')
    	{
    		//$activity_slug = bp_get_activity_root_slug();
    		//$redirect_url = $bp->root_domain."/".$activity_slug;
    		$redirect_url = $options['blr_text_field_1'];
    		return $redirect_url;
    	}
    
    	else
    	{
    		$activity_slug = bp_get_activity_root_slug();
    		$friends_activity = 	($user->ID).$activity_slug."/friends/";
    		return $friends_activity;
    	}
    }
    function blr_logout_redirect(){
      wp_redirect( home_url() );
      exit();
    }
    ?>

    https://www.ads-software.com/plugins/buddypress-login-redirect/

  • The topic ‘Added Feature: Redirect to a group of which the user is a member’ is closed to new replies.