How can I get the country a user logs in from?

edited September 2018 in WordPress

In your 'User Explorer' there is data stating "Login from United Kingdom"
How can I save this country data into the users wordpress profile when they log in to my site?

Answers

  • Claude_SchlesserClaude_SchlesserAdministratorOneAll Team

    Hello,

    you can start with a hook like this:
    http://docs.oneall.com/plugins/guide/social-login-wordpress/#3b

    The data that you are looking for is in
    $identity->loginLocation->country->short

    Regards,

  • Hi Claude,

    The following code works when registering a new user, though I wonder is it possible to update the country field every time a user logs in?

            // Handle data retrieved from a social network profile
            function oa_social_login_store_extended_data ($user_data, $identity)
            {
              // $user_data is an object that represents the newly added user
              // The format is similar to the data returned by $user_data = get_userdata ($user_id);
              // $identity is an object that contains the full social network profile
    
        $userid = $user_data->ID;
        $gender = $identity->gender;
        $birthday = $identity->birthday;
        // $locale = $identity->currentLocation;
        $logincountry = $identity->loginLocation->country->short;
        error_log(print_r($userid, true));
        error_log(print_r($gender, true));
        error_log(print_r($birthday, true));
        error_log(print_r($locale, true));
        error_log(print_r($logincountry, true));
    
      // Store the gender
         update_user_meta ($userid, 'wpcf-user-demographic-gender', $gender);
      // Store The Birthday
         update_user_meta ($userid, 'wpcf-user-demographic-birthday', $birthday);
      // Store The Country
         update_user_meta ($userid, 'wpcf-user-demographic-locale', $logincountry);
    }
    
    // This action is called whenever Social Login adds a new user
    add_action ('oa_social_login_action_after_user_insert', 'oa_social_login_store_extended_data', 10, 2);
    
  • Claude_SchlesserClaude_SchlesserAdministratorOneAll Team
    edited September 2018

    Hi James,

    for this you can use the following action/hook:

    oa_social_login_action_before_user_login

    In the variable
    $identity->loginLocation

    you can also find other properties like for example the ip address or the full country name.

  • Great, that's looks like it's worked so I have just one more question...

    If I understand correctly this country data is taken from the IP, which is outside of the social network being logged into?

    I ask so that I know how to define our web privacy terms and app requests from facebook.

  • Claude_SchlesserClaude_SchlesserAdministratorOneAll Team

    Yes, the country is based on the IP address.

Welcome!

Please sign in to your OneAll account to ask a new question or to contribute to the discussions.

Please click on the link below to connect to the forum with your OneAll account.