Skip to:
Content
Pages
Categories
Search
Top
Bottom

group for logged in user


  • howsepa
    Participant

    @howsepa

    Hoping someone can help as ive tried Sooooo.. many things for this I’m running out of ideas.

    I’m using Buddy press groups to categorize my users (office based, sales etc….)
    I’m also using a plugin called ‘Login Log’ which records logins by all users.

    The login log plugin logs many details at time of login but dosnt currently log which buddy press group a user is in. I need this infor for reporting purposes.

    No i have modified the login log plugin to record and extra field into the DB, but I am having great trouble returning the group(s) for a particular user at login time.

    The code (from the login log plugin) which retrieves the user data then stores is is below.

    you will see that i have defined a variable called ‘group’ and i need the relevant code to retrieve the buddypress group(s) to which this user belongs to so i can store it.

    Anyone shed any light on this as i have tried MANY options to no avail..

    any help greatly appreciated.

    $group =  ???????;
            
    	$values = array(
                'uid'           => $uid,
                'user_login'    => $user_login,
                'user_role'     => $user_role,
    			'user_email'		=> $user_email,
                'time'          => current_time('mysql'),
                'ip'            => isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? esc_attr($_SERVER['HTTP_X_FORWARDED_FOR']) : esc_attr($_SERVER['REMOTE_ADDR']),
    			'country'      => $guest_country,
    			'city'			=> $guest_city,
                'login_result'  => $this->login_success,
                'data'          => $serialized_data,
    			'name'			=> $USErname,
    			'group'			=> $group
                );
    
            $format = array('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s');
    
            $this->save_data($values, $format);
Viewing 3 replies - 1 through 3 (of 3 total)

  • howsepa
    Participant

    @howsepa

    Another option….

    If this wont work – i could also use custom fields in the extended profile feature of buddypress and set up a field to contain this ‘category’ information.

    Either would work.. but need to store a category for each user and capture this as they log in.


    shanebp
    Moderator

    @shanebp

    You don’t say which hook your class uses.

    Have you tried using get_group_ids( bp_loggedin_user_id() ) ?
    See the function in bp-groups\bp-groups-classes.php and note the structure of the returned array.

    If you’re hooking before BP is ready, you could always run some custom sql.
    Use SELECT COUNT via get_var.


    howsepa
    Participant

    @howsepa

    Sorry should have included full function…

    
    //Catch messages on successful login
        function login_action($user_login)
        {
    
            $userdata = get_user_by('login', $user_login);
    
            $uid = ($userdata && $userdata->ID) ? $userdata->ID : 0;
    
            $data[$this->data_labels['login']] = ( 1 == $this->login_success ) ? $this->data_labels['Successful'] : $this->data_labels['Failed'];
            if ( isset( $_REQUEST['redirect_to'] ) ) { $data[$this->data_labels['login Redirect']] = esc_attr( $_REQUEST['redirect_to'] ); }
            $data[$this->data_labels['User Agent']] = esc_attr( $_SERVER['HTTP_USER_AGENT'] );
    
            $serialized_data = serialize($data);
    
            //get user role
            $user_role = '';
            if( $uid ){
                $user = new WP_User( $uid );
                if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
                    $user_role = implode(', ', $user->roles);
                }
            }
    		$current_user = wp_get_current_user();
    		$user_email = $user->user_email;
    	
    
    		$real_client_ip_address = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? esc_attr($_SERVER['HTTP_X_FORWARDED_FOR']) : esc_attr($_SERVER['REMOTE_ADDR']);
    		
    
    		$guest_ip   = $visitor_location['IP'];
    		$guest_country = "";
    		$guest_city  = "";
    		$guest_state = "";
    		$user_info = get_userdata($uid);
            $USErname =  $user_info->first_name .  " " . $user_info->last_name;
            
    		
    
    			
    		global $bp;
    		
    		$group =  get_group_ids( bp_loggedin_user_id() );
            
    		$values = array(
                'uid'           => $uid,
                'user_login'    => $user_login,
                'user_role'     => $user_role,
    			'user_email'		=> $user_email,
                'time'          => current_time('mysql'),
                'ip'            => isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? esc_attr($_SERVER['HTTP_X_FORWARDED_FOR']) : esc_attr($_SERVER['REMOTE_ADDR']),
    			'country'       => $guest_country,
    			'city'			=> $guest_city,
                'login_result'  => $this->login_success,
                'data'          => $serialized_data,
    			'name'			=> $USErname,
    			'group'			=> $group
                );
    
            $format = array('%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s','%s','%s');
    
            $this->save_data($values, $format);
        }
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘group for logged in user’ is closed to new replies.
Skip to toolbar