Skip Navigation

[Resolved] When I create a user – it is not kept logged in

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

Problem: I have a Form that creates new Users. After submitting the Form, I would like my User to be logged in.

Solution:

add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
    
    if ( 12345 == $form_data['id'] ) {
    
        if ( !empty( $_POST['user_email'] ) && !empty( $_POST['user_pass'] ) ) {
    
            $user = get_user_by( "email", $_POST['user_email'] );
  
            $signon = array(
                'user_login'    => $user->user_login,
                'user_password' => $_POST['user_pass'],
                'remember'      => true
            );
  
            $login = wp_signon( $signon, false );
    
            if ( is_wp_error($login) ) {
                error_log( $login->get_error_message() );
            }
        }
    }
}
This support ticket is created 5 years, 7 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1229490

Tell us what you are trying to do?
I create a new user and redirect to create a new post for this author.
The form creates the user but is not loggin in and so when page is redirected to create the post access is denied.

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#1229491

You can use this code snippet to automatically log in the new User. Add the following code to your child theme's functions.php file, or create a new code snippet in Toolset > Settings > Custom code:

add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){

	if ( 12345 == $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() );
			}

		}
	}
}

Change 12345 to match the numeric ID of the new User Form.

#1229525

1st Thank you for the quick response.
I have changed the id taken from the create user form.

when it redirects to the create post it still not logged in.

#1229535

Which fields are included in your Form?

#1229537
lc-reg1.PNG

This is just a test user form will end up with just email and password

#1229546
lc-reg-828.PNG

inspect view

#1229623

Okay let's use this snippet instead, since the user_login is unknown:

add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
   
    if ( 12345 == $form_data['id'] ) {
   
        if ( !empty( $_POST['user_email'] ) && !empty( $_POST['user_pass'] ) ) {
   
            $user = get_user_by( "email", $_POST['user_email'] );
 
            $signon = array(
                'user_login'    => $user->user_login,
                'user_password' => $_POST['user_pass'],
                'remember'      => true
            );
 
            $login = wp_signon( $signon, false );
   
            if ( is_wp_error($login) ) {
                error_log( $login->get_error_message() );
            }
        }
    }
}

Change 12345 to match the Form ID and let me know if this version does not work. I'll take a closer look.

#1229640

My issue is resolved now. Thank you!