• Resolved prashantkaushik

    (@prashantkaushik)


    Hi,

    I am using Profile builder plugin. I used registration and login feature. I want when a user register it will be auto login.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Teodor Cosofret

    (@teodor-cosofret)

    Hi,

    Thank you for reaching out to us.

    If you are using the Email Confirmation feature then with some custom code we can automatically login the user after Email Confirmation:

    1. Create an empty plugin like this: https://gist.github.com/sareiodata/76f701e01db6685829db

    2. Add the following code to the end of it:

    /*
     * Profile Builder - Auto Login User after Email Confirmation. Tags auto login, email confirmation
     */
    
    add_action( 'wppb_activate_user', 'wppb_custom_autologin_redirect', 10, 3 );
    function wppb_custom_autologin_redirect( $user_id, $password, $meta ){
        
        global $current_user;
        $current_user = 0;
    
        $token = wppb_create_onetime_token( 'pb_autologin_'.$user_id, $user_id );
        $location = home_url() . "/?pb_autologin=true&pb_uid=$user_id&pb_token=$token";
        echo "<script> window.location.replace('$location'); </script>";
    }
    
    add_action( 'init', 'wppb_custom_autologin' );
    
    function wppb_custom_autologin(){
        if( isset( $_GET['pb_autologin'] ) && isset( $_GET['pb_uid'] ) &&  isset( $_GET['pb_token'] )  ){
            $uid = $_GET['pb_uid'];
            $token  = $_GET['pb_token'];
            require_once( ABSPATH . 'wp-includes/class-phpass.php');
            $wp_hasher = new PasswordHash(8, TRUE);
            $time = time();
    
            $hash_meta = get_user_meta( $uid, 'pb_autologin_' . $uid, true);
            $hash_meta_expiration = get_user_meta( $uid, 'pb_autologin_' . $uid . '_expiration', true);
    
            if ( ! $wp_hasher->CheckPassword($token . $hash_meta_expiration, $hash_meta) || $hash_meta_expiration < $time  ){
                die (' You are not allowed to do that. ');
                exit;
            } else {
                wp_set_auth_cookie( $uid );
                delete_user_meta($uid, 'pb_autologin' . $uid );
                delete_user_meta($uid, 'pb_autologin' . $uid . '_expiration');
                wp_redirect( home_url() . '/edit-profile/' );
                exit;
            }
        }
    }

    3. Install this plugin via FTP (copy it inside wp-content/plugins) or create a zip archive with it and install it via the WordPress plugin upload functionality

    Inside Profile Builder Pro we have the Multiple Registration Forms module that you could use to create custom registration forms with custom fields for custom user roles and also to Automatically Login your users.

    You can test this module on our demo install.

    Let me know if the custom code works for you.

    Best regards,

    Thread Starter prashantkaushik

    (@prashantkaushik)

    Thanks for the information.

    Actually i am not using email confirmation, user will be auto confirmed once he/she submit the form. I just create a registration form with short code. I only want that after registration he/she will be auto login in the site.

    Is above mentioned the only way to do this?

    Thanks,
    Prashant

    Thread Starter prashantkaushik

    (@prashantkaushik)

    Hi,

    This code is not working for me untill now.

    Thanks
    Prashant

    Teodor Cosofret

    (@teodor-cosofret)

    Hi,

    Yes for the moment the custom code will work if you have Email Confirmation set to Yes. Did you tested it and it didn’t work?

    Can you submit a support ticket with this issue? I will wait for your ticket and we will continue our conversation there.

    Best regards,

    Thread Starter prashantkaushik

    (@prashantkaushik)

    Hi,

    Now its work for me. I made some small changes in the code provided which works for me.

    Please find my code.
    *******************************************************************************

    //to add registration hook
    add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
    function myplugin_registration_save( $user_id ) {
        $location = home_url()."/?pb_autologin=true&pb_uid=$user_id";
        echo "<script> window.location.replace('$location'); </script>";
    }
    
    add_action( 'init', 'wppb_custom_autologin' );
    function wppb_custom_autologin(){
        if( isset( $_GET['pb_autologin'] ) && isset( $_GET['pb_uid'] )  ){
            $uid = $_GET['pb_uid'];
            wp_set_auth_cookie( $uid );
            delete_user_meta($uid, 'pb_autologin' . $uid );
            delete_user_meta($uid, 'pb_autologin' . $uid . '_expiration');
            wp_redirect( $url );
            exit;
        }
    }

    ***************************************************************************

    Thanks for the great support.
    Prashant

    Teodor Cosofret

    (@teodor-cosofret)

    Hi,

    I am glad that you managed to fix this issue with custom code.

    I wish you all the best with your project.

    Best regards,

    @prashantkaushik Can you share your full code? I’m getting php errors with the original code.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Auto login after registration’ is closed to new replies.