get_current_user_id()不能与wp_login挂钩一起使用-WordPress [英] get_current_user_id() Not Working with wp_login Hook - WordPress
问题描述
我编写了一个函数,该函数对大多数钩子(例如wp_head)都适用.问题是,我似乎无法获得用户的用户ID,因为他们使用wp_login作为我的钩子登录.
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()不返回任何内容,而且我似乎也无法获取任何其他用户对象.实际登录是否在wp_login之后进行?因为那对我来说没有意义.
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.
推荐答案
我没有让get_current_user_id()
工作,但确实找到了解决方案.
I never got get_current_user_id()
to work, but I did find a solution.
首先,我不得不给我的功能一个非常低的优先级.在动作挂钩中,我将其设置为99优先级,如下所示:
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 );
然后,我没有使用get_current_user_id()
,而是向函数添加了$login
参数,并使用了get_userdatabylogin($login)
,这为我提供了用户信息.然后就是$user_ID = $user->ID
.因此,获取ID包括以下内容:
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
}
这篇关于get_current_user_id()不能与wp_login挂钩一起使用-WordPress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!