need to get user ID after a user logs in
currently running wordpress 5.6
Ive dabbled with PHP for a long time but new to modifying wordpress. Im ultimately looking to modify the database for a user right after a user logs in if certain conditions are met. To that end im looking to get the users ID right after they log in. Ive tried to make use of the default wordpress add_action(‘wp_login’, ‘test_mail’); which as I understand it is supposed to be performed after a user logs into wordpress.
The below function gets fired off as expected when a user logs in and I get the e-mail but my userid is always 0. Ive got to be overlooking something simple any suggestions.
function test_mail(){
$user = wp_get_current_user();
$message .= "CURRENT USER ID: " . $user->ID . "n";
$to = "admin@xxxxxxxxxx.com";
$subject = "Testing mail from login";
$header = "";
$attachments = "";
wp_mail( $to, $subject, $message, $headers, $attachments );
}
add_action('wp_login', 'test_mail');
Leave an answer