WP-Mix

A fresh mix of code snippets and tutorials

WordPress Do Stuff on First User Login

Here is a code snippet that runs the first time a user logs in to their registered account. Useful for sending custom welcome emails, or whatever.

Step 1: Add User Meta on Registration

Add the following function to your plugin or theme functions.php:

function shapeSpace_register_add_meta($user_id) { 
	add_user_meta($user_id, '_new_user', '1');
}
add_action('user_register', 'shapeSpace_register_add_meta');

This adds an entry to the user meta that indicates that they are a new user. We will then be able to use this information in the next step.

Step 2: Check First Login and Do Something

At this point, after the user registers, their meta data will indicate that they are a new user. So now we can check that information and take action accordingly. Here is the second and final snippet required for this technique:

function shapeSpace_first_user_login($user_login, $user) {
	$new_user = get_user_meta($user->ID, '_new_user', true);
	if ($new_user) {
		update_user_meta($user->ID, '_new_user', '0');
		
		// do something for first login.. e.g., send a custom email
	}
}
add_action('wp_login', 'shapeSpace_first_user_login', 10, 2);

Add that to the same location as the previous snippet and add whatever functionality is desired for your user’s first login experience. Maybe send up some balloons or a bowl of ice cream to make it all special. A more realistic example would be to send a custom email message thanking the user for registering, etc.

Related Posts

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress