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.
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.The
wp_login
hook passes out of the box two extra arguments. The$accepted_args
inadd_action
has to be equal3
.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:
Then, instead of using
get_current_user_id()
, I added a$login
parameter to the function and usedget_userdatabylogin($login)
, which gave me the user information. Then it was just$user_ID = $user->ID
. So getting the ID consisted of this: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.