• Resolved ricardofcorreia

    (@ricardofcorreia)


    Hi,
    I’ve started using this plugin, auto login links works like a charm.
    I’ve also included on my functions.php the following code, in order to generate the auto-login link on user creation.

    //generate autologin links for registeret users
    function generateRandomString($length = 60) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
    add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
    function myplugin_registration_save( $user_id , $length ) {
    	$meta_key = "pkg_autologin_code";
    	$timestampz=time();
    	$tokenparta = generateRandomString();
    	$key = $timestampz*3 . $tokenparta;
    	update_user_meta( $user_id, $meta_key, $key );
    }

    What I need now, is the ability to send the autologin link on the user registration automatic email, so the user will be able to follow that link and automatically sign in on the website.

    How can we adapt this code to to this?

Viewing 1 replies (of 1 total)
  • Thread Starter ricardofcorreia

    (@ricardofcorreia)

    Hi,

    Been looking throughout the code and implemented a solution that seems to work for my personal needs.

    Here’s the code:

    //generate autologin links for registered users
    function generateRandomString($length = 60) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
    
    add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
    function myplugin_registration_save( $user_id , $length ) {
    	global $meta_key;
    	global $my_array;
    	$my_array['ID'] = $user_id;
    
    	if ( isset(  $_POST['user_login'] ) ) {
    		$my_array['user_login'] =  $_POST['user_login'];
    	} else {
    		$user_info = get_userdata( $user_id );
    		$my_array['user_login'] = $user_info->user_login;
    	}
    
    	$meta_key = "pkg_autologin_code";
    	$my_array['mkey'] = $meta_key;
    	$timestampz=time();
    	$tokenparta = generateRandomString();
    	$key = $timestampz*3 . $tokenparta;
    	$my_array['kkey'] = $key;
    
    	update_user_meta( $user_id, $meta_key, $key,  $my_array['mkey'], $my_array['kkey']);
    }
    
    add_filter( 'wp_mail', 'send_auto_login_link' );
    function send_auto_login_link( $atts ) {
    	if ( isset ( $atts ['subject'] ) && $atts['subject'] = 'Your username and password' ) {
    		if ( isset( $atts['message'] ) ) {
    			global $my_array;
    			$qstr = '?autologin_code=' . $my_array['kkey'];
    			$old = '/wp-login.php';
    			$new = '/' . $qstr;
    			$atts['message'] = str_replace( $old, $new, $atts['message'] );
    		}
    	}
    	return $atts;
    }

    I’ve used the shared code for auto generate the autologin link on user registration, and created an override for the user registration email, that gets an array of info, like the user_id, and key to replace the original message with user info and autologin link.

    Let me know if it works for you too.
    Thanks!

Viewing 1 replies (of 1 total)
  • The topic ‘Send autologin link on registration email’ is closed to new replies.