Make user autojoin to the current Group if They register from group page using Bp Ajax registration plugin
class BPAjaxR_Autojoin_Current_Group{
private static $instance;
private function __construct() {
add_action( 'bp_after_account_details_fields', array( $this, 'add_group_field' ) );
add_action( 'bp_signup_usermeta', array( $this, 'update_signup_meta' ) );
add_action( 'bp_core_activated_user', array( $this, 'join_on_activation' ), 10, 3 );
}
/**
* Get the singleton instance
* @return BPAjaxR_Autojoin_Current_Group
*/
public static function get_instance(){
if( !isset ( self::$instance ) )
self::$instance = new self();
return self::$instance;
}
/**
* add current group as hidden field on registration page
*/
public function add_group_field() {
if( !bp_is_group() )
return ;
echo "<input type='hidden' name='current-group' value='". bp_get_group_id( groups_get_current_group() )."' />";
}
/**
* Add the group field to signup meta
* @param type $meta
*/
public function update_signup_meta( $meta ) {
//check if the group field is set
if( $_POST['current-group'] )
$meta['group_to_join'] = (int) $_POST['current-group'];
return $meta;
}
/**
* Make user join the group when account is activated
* @param type $user_id
* @param type $key
* @param type $user
*/
public function join_on_activation( $user_id, $key, $user ) {
$meta = $user['meta'];
if( empty( $meta['group_to_join']))
return ;
$group_id = (int) $meta['group_to_join'];
groups_join_group($group_id, $user_id );
}
}
//initialize
BPAjaxR_Autojoin_Current_Group::get_instance();
Please put the above code in bp-custom.php
#bp-hacks, #registration #bp-ajax-registration