Action uncanny-learndash-toolkit

wp_login

Fires immediately after a user successfully logs into the WordPress site, passing the user object.

add_action( 'wp_login', $callback, 10, 2 );

Description

Fires immediately after a user successfully logs in. Developers can use this hook to perform actions such as redirecting users, updating user meta, sending notifications, or integrating with other services upon successful authentication. It provides access to the user object for custom logic.


Usage

add_action( 'wp_login', 'your_function_name', 10, 2 );

Parameters

$user (mixed)
This parameter contains the user object for the user who has just successfully authenticated.
$user (mixed)
This parameter contains the user object of the currently logged-in user.

Examples

/**
 * Fires after a user has successfully logged in.
 *
 * @param string $user_login The username of the logged-in user.
 * @param WP_User|WP_Error $user   The WP_User object of the logged-in user, or WP_Error on failure.
 */
add_action( 'wp_login', 'my_custom_login_handler', 10, 2 );

function my_custom_login_handler( $user_login, $user ) {
    // Check if the user object is valid and not a WP_Error.
    if ( is_a( $user, 'WP_User' ) ) {
        // Log a message to the debug log indicating a successful login.
        // This is useful for debugging and tracking user activity.
        error_log( "User '" . $user_login . "' successfully logged in. User ID: " . $user->ID );

        // You could also perform other actions here, such as:
        // - Updating a user meta field (e.g., 'last_login_date' => current_time( 'mysql' )).
        // - Sending a notification email to an administrator.
        // - Redirecting the user to a specific page after login (though this is usually handled by WP redirects or other plugins).

        // Example: Update last login timestamp.
        update_user_meta( $user->ID, 'last_login_timestamp', time() );
    } else {
        // Log an error if the user object is not valid.
        error_log( "wp_login action triggered with invalid user object for login: " . $user_login );
    }
}

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

src/includes/two-factor/providers/wp-2fa/class-authentication-handler.php:215

private function handle_successful_2fa( $user_id, $result ) {
		$user = get_user_by( 'id', $user_id );

		if ( ! $user ) {
			wp_die( esc_html__( 'Authentication error. Please try logging in again.', 'uncanny-learndash-toolkit' ) );
		}

		// Actually log the user in.
		wp_clear_auth_cookie();
		wp_set_current_user( $user_id );
		wp_set_auth_cookie( $user_id );

		// Clear the 2FA cookie since authentication is complete.
		$this->cookie_manager->clear_2fa_cookie();

		// Trigger login action for other plugins.
		do_action( 'wp_login', $user->user_login, $user );

		// Redirect to success page.
		$this->redirect_manager->redirect_to_success( $result );
	}

Scroll to Top