php - Can't access user details with get_user_by or wp_get_current_user function in WordPress on wp_login hook -
i'm building plugin needs user's name , email when log in wordpress site. i've tried using both get_user_by()
, wp_get_current_user()
functions in wordpress no avail. i'm using wp_login
action hook.
example of code:
function send_login_notification() { $user_info = get_user_by('id', get_current_user_id()); $user_name = ucfirst($user_info->user_login); $user_email = $user_info->user_email; [...] } add_action('wp_login', 'send_login_notification');
however, presented following error sometimes, not always:
notice: trying property of non-object in [file path] on line 48 notice: trying property of non-object in [file path] on line 49
if refresh, chances work, strange. hit-and-miss.
i don't have experience of particular hook, sounds uses session / cookie hold user details. in case first call happening before session created/available. however, on refresh session available , code works.
have tried setting priority on action ensure occurs late possible? events have priority of 10 setting action somewhere after job.
add_action('wp_login', 'send_login_notification', 15, 2);
if doesn't work, should take @ codex or here indicates authenticate user yourself, can build user data manipulate own code.
$user = wp_authenticate($username, $password);
hopefully, adding line make difference.
it seems neither of above methods sufficient ensure login session available script, small change function required make use of data available function via wp_login.
function send_login_notification($username, $password) { $user_info = get_user_by('login', $username);
Comments
Post a Comment