New to wpquestions.com . Sorry in advance if I do anything wrong here.
function wpufe_auto_login_new_user( $user_id ) {
wp_set_current_user( $user_id );
wp_set_auth_cookie( $user_id, false, is_ssl() );
}
add_action( 'user_register', 'wpufe_auto_login_new_user' );
I'm using the above function to automatically login a person after registration. It is for a specific use case and I would like to make it only work for a <strong>particular page or template where the form exists. (not all login forms as it works now.)</strong> Would this be possible? I am using wp_login_form() in a page template for a unique login form.
I've tried the below conditional as a page id as well as a page name 'My Page' and it makes the action not fire at all over all forms on the site.
function wpufe_auto_login_new_user( $user_id ) {
if ( is_page( 3943 )) {
wp_set_current_user( $user_id );
wp_set_auth_cookie( $user_id, false, is_ssl() );
}
}
add_action( 'user_register', 'wpufe_auto_login_new_user' );
If it is not possible, and you state why it is not possible, I would accept that answer.
Thanks.
Rempty answers:
Try adding a custom form field to your register form
<input type="hidden" name="page_ref_id" value="3943" />
And modify your add_action
function wpufe_auto_login_new_user( $user_id ) {
if (isset($_POST['page_ref_id']) && $_POST['page_ref_id']=='3943') {
wp_set_current_user( $user_id );
wp_set_auth_cookie( $user_id, false, is_ssl() );
}
}
add_action( 'user_register', 'wpufe_auto_login_new_user' );
WpTraveler comments:
Hey. This looks like the answer for me. I will try and vote this one as the answer. Hopefully, I won't mess it up. The little "how to assign prize money" tutorial link seems to be broken. First time user here.
Andrea P answers:
do these users have anything else different from the others?
I mean, when they register using that form, do they also get a specific role or a specific custom meta field value?
because I think a good way to go would be retrieve that role or meta, using the $user_id and then conditionally log them in or not.
so something like:
if they have that role or meta, means they are that kind of users who use that form to register, hence we log them automatically
is this applicable to your scenario?
WpTraveler comments:
All the new registrants have the same role. So, I don't think this would work for me. I think Rempty above looks like the right answer for me.
Thanks for your comment though.
dimadin answers:
Reason why your code isn't working is probably because that function happens before vars are set so is_page() can't work. One option is to do what Rempty proposed, but I would change it do be more abstract, not just tied to page ID.
So in short, pass something more from your registration form and then check for existence of that in your function where you want to redirect.