Sign in the new user after Checkout

How can I sign in the new user after the checkout?

The current workflow is not very user friendly. After the checkup the user need to sign is to access the content.

What I do is this:

  1. Create a file called s2-auto-login.php
  2. Paste in the following text:
<?php
add_action('ws_plugin__s2member_during_configure_user_registration', 's2_auto_login_after_registration');

function s2_auto_login_after_registration($vars = array()) {
    if(is_admin()) return; // Not when an Admin is creating accounts.
		wp_set_auth_cookie($vars['user_id'], FALSE, FALSE); // Log the user in.
    
    if(did_action('login_form_register')) // For `/wp-login.php?action=register` compatibility.
		c_ws_plugin__s2member_login_redirects::login_redirect($vars['login'], $vars['user']);
    
    $GLOBALS['_s2_auto_login_after_registration_vars'] = $vars; // For Pro Form compatibility.
    
    add_action('template_redirect', '_s2_auto_login_after_registration', 1);
}
function _s2_auto_login_after_registration() // Pro Form redirection handler.    
	{
    $vars = $GLOBALS['_s2_auto_login_after_registration_vars'];
    c_ws_plugin__s2member_login_redirects::login_redirect($vars['login'], $vars['user']);
    wp_redirect( home_url( '/welcome/' ) );
    exit;
}

Then upload that file to the mu-plugins folder.

What this will do is automatically login a user as soon as they have submitted the registration form. As you say, this is much more user-friendly than having the user wait for emails and click on links.

Of course, you can change the penultimate line to have the user redirected to a page with a different name if you’d prefer.

1 Like

Thank you,
will this also work with TML? (Theme my Login)?.
From the code I don’t see anything specific to.

No idea. I don’t like Theme My Login at all. The approach is all wrong in my view. And, last I heard, TML was having problems working with s2Member anyway.

I always use Clean Login for my logins. That way, I can just put a shortcode on any page, and it’s already themed.

Hello Tim can you elaborate on the TML alternative a bit more?

Not sure there’s much to say. Clean Login is available here: https://wordpress.org/plugins/clean-login/

You just create a page in the normal way, put a shortcode on it, and you have your login page. Then you can, if you wish, set up a redirect so that wp-login.php redirects to your new login page

In order to have a page that is easy to edit, we elected not to use /wp-login.php?action=register but rather a page like our user Levels 1-4 (see https://www.familyofficeimpactinvesting.net/membership-options/free-membership/). It uses this code for the new member registration: [s2Member-Pro-Stripe-Form register=“1” level=“0” ccaps="" desc=“Free Member” custom=“www.familyofficeimpactinvesting.net” tp=“0” tt=“D” captcha=“clean” /]

When I use the code described above (which I also got from Jason at https://s2member.com/kb-article/auto-login-on-registration), it does not auto login. I get a WP login screen with the new user name already inserted and a prompt to give my password in order to login.

Can anyone help in suggesting how to modify the auto login code above to work with my configuration. Thanks in advance. Michael

@sauvante: My code is NOT the same as on the page to which you link. I added a couple of lines at the end precisely to make it work.

Thanks Tim. Let me try that.

You are the man Tim. That worked perfectly. Thanks so much.

1 Like

Hello Tim!

I also used your code from above, and put the file in mu plugins directory. Wordpress sees the plugin, but it does nothing when i go through the checkout process. I have checked the script for typing errors, syntax errors, and have replaced your redirect url with my own.

Is there anything else that need to be done to get the action to fire when the checkout is done?

Just to say that Tim’s code works perfectly ‘as is’ above.

Ronny, daft question - but have you made sure you uploaded the script in plain text?

And the redirect needs to only have the last part of the slug - not the full www. etc.

Just /mypage/

Works great for me.

Yes, it is text. I have several other, plain php files, that triggers like it should as a payment notification. But none of my mu-plugins fires, weird…

I’m not that fluent in wordpress coding, but is there any good way to debug mu-plugin style scripts?

Create debug outputs of what you need to see.

I’ve noticed when using this code, I don’t receive the new user registration admin notification email?

Is that expected behaviour with this script?

No, it isn’t. I get an email every time.

I get the S2 Member registration email - but not the regular WP new user email.

I get both when I don’t use the code as it is above?

Do you get both emails @KTS915?

You have said that you “don’t receive the new user registration admin notification email,” nor “the regular WP new user email,” but that you do “get the S2 Member registration email.”

I am not sure whether you are referring to three different emails, or are using two different labels to mean the same thing (which would mean that you are referring to two emails).

What I receive as admin are two emails: one from the payment processor (confirming payment) and one from the site (confirming a new user registration). That’s what I’d expect.

@KTS915 - sorry, I wasn’t very clear there.

Without this code, if a free member signs up to my site I get 2 emails.

One is an S2 Member ‘registration email’, and one is a WP ‘new user’ email.

If they are signing up for S2 level 1, I’ll also get an email from my payment gateway - i.e. Paypal.

WITH this code in place, I still get the S2 registration email, and a payment provider email, if premium, but NO standard WP ‘new user’ email.

I hope that’s a bit clearer?

The next time a user signs up, I’ll screenshot the emails if that helps?

Thanks again, it’s much appreciated.

OK, that sounds like what I’m getting. (I don’t really want another email!)