Action
uncanny-automator-pro
wp_login
Fires after a user successfully logs into WordPress, providing user data for integration.
add_action( 'wp_login', $callback, 10, 2 );
Description
Fires after a user has successfully logged in. Developers can use this to trigger actions, send notifications, or log login activity. The hook receives the username, the WP_User object, and a boolean indicating if the login was "remembered".
Usage
add_action( 'wp_login', 'your_function_name', 10, 2 );
Parameters
-
$user(mixed) - This parameter is the username of the user who has just logged in.
-
$user(mixed) - This parameter contains the username of the user who just logged in.
Examples
/**
* Example function to hook into the 'wp_login' action.
* This function will log a message to the debug log when a user logs in.
*
* @param string $user_login The username of the logged-in user.
* @param WP_User $user The WP_User object of the logged-in user.
* @param bool $redirect_to Whether the user was redirected. (This parameter is often true/false, but can sometimes be a URL string).
*/
function my_custom_wp_login_handler( string $user_login, WP_User $user, bool $redirect_to ): void {
// Check if the user object is valid and has an ID.
if ( ! $user instanceof WP_User || ! $user->exists() ) {
return;
}
// Log a message to the WordPress debug log.
error_log( sprintf(
'User "%s" (ID: %d) has successfully logged in.',
$user_login,
$user->ID
) );
// You could also perform other actions here, like:
// - Update user meta data.
// - Trigger other custom actions or filters.
// - Send a notification email (though be mindful of performance on every login).
// For demonstration, let's add a custom user meta if it doesn't exist.
if ( ! get_user_meta( $user->ID, 'last_login_timestamp', true ) ) {
update_user_meta( $user->ID, 'last_login_timestamp', time() );
}
}
// Add the action hook with the appropriate number of arguments.
// The 'wp_login' hook passes 3 arguments: $user_login, $user, $redirect_to.
add_action( 'wp_login', 'my_custom_wp_login_handler', 10, 3 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
uncanny-automator-pro/src/core/includes/automator-pro-deferred-user-login.php:227
private static function login_user( int $user_id, WP_User $user ): void {
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login, $user, false );
}