form – auto-login on register

Sometimes the user have a smoother experience if it is possible to login automatically after registration. For example, I’ve used this functionality on a directory website where registered users can publish their own business.

On the add business page, if the user is logged in, I display my “add new business” Toolset Form, if the user is not logged in, I display the login form and a Toolset User Form to register new users.

Instead of reload the login form after register, I can reload the page, then auto-login the user and display the my “add new business” Toolset Form.

I can do this with this function called by a cred_save_data action:

/**
* Auto-login new Toolset User Form
*/
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );

function tssupp_cred_autologin( $post_id, $form_data ){

  if ( $form_data['id'] == 55 ) { // 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() );
      }

    }
  }
}


wp_signon() reference: https://codex.wordpress.org/Function_Reference/wp_signon
is_wp_error() reference: https://codex.wordpress.org/Function_Reference/is_wp_error

Remember to change “$form_data[‘id’] == 55” with your Toolset User Form ID.

You can find this solution on this Toolset forum thread:

https://toolset.com/forums/topic/automatically-log-in-user-after-created/