1

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 = "[email protected]";
    $subject = "Testing mail from login";
    $header = "";
    $attachments  = "";
    wp_mail( $to, $subject, $message, $headers, $attachments );         
}

add_action('wp_login', 'test_mail');

1 Answer 1

1

wp_login is passed two params, but you will need to update your hook to pass them correctly to your function:

function test_mail( $user_login, $user ) {
    //$user = wp_get_current_user();
    $message .= "CURRENT USER ID: " . $user->ID . "\n";
        
    $to = "[email protected]";
    $subject = "Testing mail from login";
    $header = "";
    $attachments  = "";
    wp_mail( $to, $subject, $message, $headers, $attachments );      
}
add_action('wp_login', 'test_mail', 10, 2);

Read more here: https://developer.wordpress.org/reference/hooks/wp_login/

1
  • 1
    Thank you very much!
    – Rubicon14
    Commented Dec 28, 2020 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.