get_current_user_id() Not Working with wp_login Ho

2019-04-14 23:37发布

I wrote a function and it works great with most hooks, such as wp_head. The problem is, I can't seem to get the user ID for a user, as they log in with wp_login as my hook.

get_current_user_id() returns nothing, and I can't seem to get any other user object either. Does the actual login happen AFTER wp_login? Because that doesn't make sense to me.

4条回答
Viruses.
2楼-- · 2019-04-14 23:49

There are 4 different ways:

add_action('wp_login', 'your_function');
add_action('wp_authenticate', 'your_function');
add_filter('authenticate', 'your_function', 77, 3); add_filter('wp_authenticate_user', 'your_function', 77, 3);

I like authenticate filter.

add_filter( 'authenticate', 'my_login_checker', 91, 3 );      // https://codex.wordpress.org/Plugin_API/Filter_Reference/authenticate
function my_login_checker($user_data=false){
            // 'authenticate'  returns $user_data OBJECT: WP_Error (i.e. http://pastebin.com/raw/5A6LsiJW )  or WP_User ( http://pastebin.com/raw/TE2v22LT )
            // it detects (before user finally authorized) if during login an error already happened (i.e. password was incorect or etc..)   */
  // var_dump($user_data);exit;

    //if authorization was already INCORRECT till this function
    if(!empty(!$user_data->errors)){
        // Actually, no need to do anything.. the object is error itself, so WP will do itseft...
    }
    //else, if authorization was OK  yet..
    else{
        if($_SERVER['REMOTE_ADDT'] != '123.123.123.123'){
            $user_data= new  WP_Error('my_ip_fail_titlee', __('<span style="color:red;font-size:1.3em;">ERROR - I DONT LIKE YOUR IP!</span>'));
        }
    }
    return $user_data;
}
查看更多
叛逆
3楼-- · 2019-04-14 23:50

The wp_login hook passes out of the box two extra arguments. The $accepted_args in add_action has to be equal 3.

add_action('wp_login', 'my_function', 10, 3);

/**
* @param string $user_login
* @param \WP_User $user
*/
public static function start_session($user_login, $user)
{
  do_something_with_user_login($user_login);
  do_something_with_user_id($user->ID);
}
查看更多
Melony?
4楼-- · 2019-04-15 00:00

I never got get_current_user_id() to work, but I did find a solution.

First, I had to give my function very low priority. In the action hook, I gave it 99 priority, like so:

add_action( 'wp_login', 'my_function', 99 );

Then, instead of using get_current_user_id(), I added a $login parameter to the function and used get_userdatabylogin($login), which gave me the user information. Then it was just $user_ID = $user->ID. So getting the ID consisted of this:

function my_function( $login ) {
    $user = get_user_by('login',$login);
    $user_ID = $user->ID;
    //do something with the User ID
}
查看更多
5楼-- · 2019-04-15 00:07

Their is official Documentation about how to get user id.

http://codex.wordpress.org/Function_Reference/get_currentuserinfo

This is code which i made from Documentation.

if ( is_user_logged_in() ) {
global $current_user;
  get_currentuserinfo();

  echo 'Username: ' . $current_user->user_login . "\n";
  echo 'User email: ' . $current_user->user_email . "\n";
  echo 'User first name: ' . $current_user->user_firstname . "\n";
  echo 'User last name: ' . $current_user->user_lastname . "\n";
  echo 'User display name: ' . $current_user->display_name . "\n";
  echo 'User ID: ' . $current_user->ID . "\n";
}
else {
echo "Welcome Guest!";
}
查看更多
登录 后发表回答