WP-Members: Automatically login after registration

WP-Members is a fantastic plugin for securing various areas of your WordPress site for access by registered users.  While extremely versatile, not everything can be controlled through the settings alone.

An often-requested feature is for the plugin to automatically login a user after they have registered.  Unfortunately, this functionality is unavailable in the current version of the plugin.  However, by adding an action to WP-Members’ wpmem_post_register_data tag, it becomes possible to add this extra functionality.  The following can be added to your plugin file, or even to your theme’s functions.php file:

add_action('wpmem_post_register_data', 'my_registration_hook', 1);

function my_registration_hook($fields) {
	$user_login = $fields[username];
	$user_id = $fields[ID];

	wp_set_current_user($user_id);
	wp_set_auth_cookie($user_login);
	do_action('wp_login', $user_login);

	wp_set_current_user($fields[ID]);
}

 

Note: At the time of writing, the current production version of WP-Members is 2.8.2