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

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 5 replies, has 5 voices.

Last updated by rafalM 2 years, 1 month 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

Beda
Supporter

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

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

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.