[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 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.

This topic contains 8 replies, has 6 voices.

Last updated by smileBeda 2 years, 4 months ago.

Assigned support staff: 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!