do_action( 'wp_login', string $user_login, WP_User $user )

Fires after the user has successfully logged in.


Parameters Parameters

$user_login

(string) Username.

$user

(WP_User) WP_User object of the logged-in user.


Top ↑

More Information More Information

The wp_login action hook is triggered when a user logs in by the wp_signon() function. It is the very last action taken in the function, immediately following the wp_set_auth_cookie() call.

This hook provides access to two parameters: $user->user_login (string) and $user ( WP_User ). To pass them into your function you will need to add a priority (default is 10) and request 2 arguments from the add_action() call:

<?php
function your_function( $user_login, $user ) {
    // your code
}
add_action('wp_login', 'your_function', 10, 2);
?>

Top ↑

Source Source

File: wp-includes/user.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
1.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by pixelandgrain

    Sending a welcome email to first time logged in users.

    // callback function
    function wpdocs_send_welcome_email( $user_login, WP_User $user ) {
    
        // do not send email if user has already logged in once
        if ( current_user_can( 'administrator' ) || get_user_meta( $user->ID, 'wpdocs_welcome_email_sent', TRUE ) ) {
            return;
        }
    
        // send welcome email if logging in first time
        $message = str_replace(
            array( '%%firstname%%', '%%name%%', '%%sitename%%' ),
            array(
                $user->first_name,
                $user->data->user_login,
                get_bloginfo( 'name' ),
            ),
            get_option( 'welcome_message' )
        );
    
        // send email to user
        if ( wp_mail( $user->data->user_email, 'Welcome subject', $message ) ) {
            // update or add user meta if email sent successfully
            update_user_meta( $user->ID, 'wpdocs_welcome_email_sent', 1 );
        }
    
    }
    
    // action hook
    add_action( 'wp_login', 'wpdocs_send_welcome_email', 10, 2 );

You must log in before being able to contribute a note or feedback.