Skip Navigation

[Resolved] Auto log in after sign up with user form

This support ticket is created 5 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Author
Posts
#1208150

I have a created a new user sign up form with a custom role created in Access.

When the user signs up the also get a email notification with their log in details, they are also redirected to My Account.

My account is restricted by Access and redirects to a log-in page if not logged in.

Is there a way either in Toolset or a snippet to auto login so the new user automatically goes into My Account

Many thanks

#1208531

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Set up your user registration form so that it redirects to the desired page.

Then add the following code to your site (you can add it at Toolset > Settings > Custom Code):

/**
 * Auto-login new CRED user
 */
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){

	if ( 6 == $form_data['id'] ) { // Edit as required

		if ( !empty( $_POST['user_login'] ) && !empty( $_POST['user_pass'] ) ) {

			// get the user credentials from the $_POST object
			$user = array(
				'user_login'	=>	$_POST['user_login'],
				'user_password'	=>	$_POST['user_pass'],
				'remember'		=>	true
			);
			$login = wp_signon( $user, false );

			if ( is_wp_error($login) ) {
				error_log( $login->get_error_message() );
			}
		}
	}
}

You'll need to edit the ID of the form.

#1208556

Hi Nigel
Thanks for the help

My form id is

I have inserted code below into my
1) functions.php file and made no difference
2)Settings>Custom code and again made no difference

ie the user is taken to my-account by then is show then the log in logged (which is the access rule)

/**
 * Auto Login
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );


add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
 
    if ( 6 == $form_data['26936'] ) { 
 
        if ( !empty( $_POST['user_login'] ) && !empty( $_POST['user_pass'] ) ) {
 
            // get the user credentials from the $_POST object
            $user = array(
                'user_login'    =>   $_POST['user_login'],
                'user_password' =>   $_POST['user_pass'],
                'remember'      =>   true
            );
            $login = wp_signon( $user, false );
 
            if ( is_wp_error($login) ) {
                error_log( $login->get_error_message() );
            }
        }
    }
}

Im not sure if I understand what the value 6 is at the beginning of the of statement

#1208642

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

6 is the id you should have changed, so if the id of your registration form is 26936 then that line should be

    if ( 26936 == $form_data['id'] ) { 
#1208691

Hi Nigel
This os the code I have used and still not redirecting

<?php
/**
 * New custom code snippet.
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
 
    if ( 26936 == $form_data['id'] ) { // Edit as required
 
        if ( !empty( $_POST['user_login'] ) && !empty( $_POST['user_pass'] ) ) {
 
            // get the user credentials from the $_POST object
            $user = array(
                'user_login'    =>   $_POST['user_login'],
                'user_password' =>   $_POST['user_pass'],
                'remember'      =>   true
            );
            $login = wp_signon( $user, false );
 
            if ( is_wp_error($login) ) {
                error_log( $login->get_error_message() );
            }
        }
    }
}

Could you take a quick look if I sent you access details?

#1209448

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Yes, sure, I have set up a private reply for you...

What is the page with the registration form?

#1209522

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Caspar

The first problem was that you had added the code snippet but not activated it, so it wasn't running.

I did that, but saw that I was still locked out of the my-account page.

I thought maybe I needed to add a slight delay to the redirect in the form to give the log in time before hitting the page that tested to see if the user were logged in, but that gave unexpected results.

I needed to do some more testing for this specific scenario where your target page is protected by Access.

I'll see if I can get it working locally then I'll be back in touch.

#1209875

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Caspar

I realised the problem is that your form automatically generates the username which isn't included in the form directly.

That meant some minor changes to the code which I made and tested successfully on your site.

For the record, this is what I changed it to:

add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
 
    if ( 26936 == $form_data['id'] ) { // Edit as required
 
        if ( !empty( $_POST['user_email'] ) && !empty( $_POST['user_pass'] ) ) {
 
            // get the user credentials from the $_POST object
            $user = array(
                'user_login'    =>   $_POST['user_email'],
                'user_password' =>   $_POST['user_pass'],
                'remember'      =>   true
            );
            $login = wp_signon( $user, false );
 
            if ( is_wp_error($login) ) {
                error_log( $login->get_error_message() );
            }
        }
    }
}
#1209884

Hi
Yes sorry I have probably disabled the snippet - rather than having a function running that wasnt being used.

Thanks for the amended code O I am out this mornign but will look this aternoon

It is a pain as yes they dont want a username as such but to use the email as a username.
I have also have to make custom fields for first name and last name as these needed to mandatory so I can see how any deviation from a normal user account could impact other things such as this

#1209897

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Not a problem to use the email field to automatically generate the username.

I registered a test user and it works correctly, redirecting to the My Account page, logged in.

#1209923

Excellent thank you very much -it works
So one final question about this - is there any different between using this code as a toolset snippet or in functions.php?
(as this i where I would normally add such code)

Thanks

#1209925

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

If you add code to your theme's functions.php file then switching theme would mean losing your code.

That's what the code snippets with Toolset is set to avoid.

#1209934

Ok thanks, other than this though no odifference in performance or the way it loads in to the code or permissions or anything like that between the two?

Cheers

#1617881

Hi Nigel, I'm also having the same issues.

I'm using the code in the functions.php file of my child theme, as when I try to add it as a snippet in Toolset settings it crashes my site:

function tssupp_cred_autologin( $post_id, $form_data ){
  
    if ( 3796731 == $form_data['id'] ) { // Edit as required
  
        if ( !empty( $_POST['user_email'] ) && !empty( $_POST['user_pass'] ) ) {
  
            // get the user credentials from the $_POST object
            $user = array(
                'user_login'    =>   $_POST['user_email'],
                'user_password' =>   $_POST['user_pass'],
                'remember'      =>   true
            );
            $login = wp_signon( $user, false );
  
            if ( is_wp_error($login) ) {
                error_log( $login->get_error_message() );
            }
        }
    }
}
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );

Has something changed in an update?