Skip Navigation

[Resolved] Automatically log-in user after created

This thread is resolved. Here is a description of the problem and solution.

Problem

I want an user to be logged in automatically after he/she created the account on my Website, using Toolset CRED.

Can this be done with Toolset?

Solution

Not without the effort of some Custom Code.

We have a great example linked below.

100% of people find this useful.

This support ticket is created 6 years, 9 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.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 10 replies, has 7 voices.

Last updated by rubenB 6 years, 9 months ago.

Assisted by: Beda.

Author
Posts
#621405

I am trying to create a house listing site.

The idea is, once a user registers, he/she should be able to submit their first listing, WITHOUT the need to re-login again.

This is the typical functionality in any site where registration is needed: the moment you register you appear to be logged in into the system.

Cannot understand why in Toolset the user needs to be re-directed to a login form after registration!? If he has just registered why not make him log in automatically?

Does this make sense? 🙂

Thanks

#621650

It's because this is how WordPress works.

Anyway, I think there is a solution for you.

1. Create a CRED User form
2. It should redirect to a page after submission (to trigger a page reload.)
3. Then add the following code to your theme’s functions.php file or using a plugin such as Code Snippets, and adjust the ID of the CRED form in the 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() );
			}

		}
	}
}

This is custom code and not subject to Support, but since it is possible using a CRED Hook, we help you providing examples like the above.
The Document for the CRED Hook is here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

The rest of the code above mainly uses native PHP or WordPress API.

Here a few of the most important used:
$_POST: hidden link
empty: hidden link
wp_signon: https://codex.wordpress.org/Function_Reference/wp_signon
is_wp_error: https://codex.wordpress.org/Function_Reference/is_wp_error

Please let me know if this helps you to achieve your goal.

#621729

Hi Beda,

Your solution works perfect!! 🙂

Thanks a lot for your detailed explanation and code, very much appreciated.

All the best.

Alvaro

#1491801

Hi there,

This code doesn't work for me; hidden link

It's redirects, but doesn't login.

Was this feature phased out?

#1504495

Also can't get this to work with the latest WordPress
Assuming to replace the 6 id like this,

if ( 9999 == $form_data['id'] ) { 

also tried other variations... any clues or updates?

Registering and logging in at the same time is a user friendly experience that people are used to nowadays, logging in again is less friendly.

#1510907

It still works.
In some cases like my, besides the id that needs to be changed the other one is the field with login/email value. If you dont use login and instead email than $_POST['user_login'] in 2 places should be changed to $_POST['user_email']. In wordpress users can login with login or email, so that could be the issue here.

#1617985

Did you ever figure this out RubenB?

#1619413

No sadly I haven't figured it out Kyle :/ Have you?

#1667943

Hello @kyleh-2, @rubenb, @rafalm

I am sorry I never received your follow-ups while I was still Toolset Supporter (this is because the ticket was resolved), however, guess what, I found my own ticket here in the forum because I had a doubt about the correct setup... Things come around 😀

The Code is functional and still works.

You need to ensure you amend it to target the right Form, and you will want to put this code in your functions.php file.
Also, the username in my case is autogenerated by Toolset Forms.
I allow the visitor only to add Last and First Name, Email and Password.

If you need help with this, I can help, however since I am not anymore Supporter at Toolset, we'd need to work out something offsite: you may contact me at https://www.tukutoi.com/contact.

Cheers!

#1946487

I found if you do not have the 'user_login' on the CRED form then you will need to adjust the function - see below.

IF this isn't not the form: [cred_field field='user_login' class='form-control' output='bootstrap']

add_action( 'cred_save_data', 'cred_autologin_36', 10, 2 );

function cred_autologin_36( $post_id, $form_data ){

if ( 63 == $form_data['id'] ) { // Edit as required
// change $_POST['user_login'] to user_email
if ( !empty( $_POST['user_email'] ) && !empty( $_POST['user_pass'] ) ) {

// get the user credentials from the $_POST object
$user = array(
// change $_POST['user_login'] to user_email
'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() );
}

}
}
}

In case it helps anyone.

#2022913

Thanks so much for following up on this @smileBeda,
That works nicely for my site now, thanks for your well explained snippet.
I don't really remember what i had done wrong before though!

I really like your website BTW, I might get in touch there for some other coding help 🙂