I’ve tried to do automatic login to wordpress after registering with password when using WP-Members plugin.
I don’t have a blog so I decided to write it here, so it may be helpful for someone. I’m not really good in programming wordpress plugins. And it took me some time to find it.
Of course that works if you add custom field (option_name = password) in the plugin’s settings.
In file wp-members.register.php in function *wpmem_registration( $toggle )* I’ve replaced this line:
return "success"; exit();
with that code from function *wpmem_login()* in file wp-members-core.php :
/** assemble login credentials */
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = true;
/** wp_signon the user and get the $user object */
$user = wp_signon( $creds, false );
/** if no error, user is a valid signon. continue */
if( ! is_wp_error( $user ) ) {
/** set the auth cookie */
wp_set_auth_cookie( $user->ID, true );
/** determine where to put the user after login */
$redirect_to = $_SERVER['PHP_SELF'];
if( isset( $_POST['redirect_to'] ) ) {
$redirect_to = $_POST['redirect_to'];
}
/** apply wpmem_login_redirect filter */
$redirect_to = apply_filters( 'wpmem_login_redirect', $redirect_to );
/** and do the redirect */
wp_redirect( $redirect_to );
/** wp_redirect requires us to exit() */
exit();
} else {
return "loginfailed";
}
Is this safe?
Could it be done in some better way?
I’m not sure I follow you correctly, but you shouldn’t be editing core plugin files, instead you should make use of the available hooks and filters provided by WP-Members and for example either create your own plugin or more simply use your functions.php file to modify its behavior.
Also taken from this link,
It appears to be an option within the plugin itself to either moderate or not moderate registrations, the latter of the two will auto-login a user on successful registration while the former, if turned on, will require that the user activate their account via e-mail or that an admin approve their account.