0
votes

In my wordpress website I have created a custom registration page to register in Wordpress using "PHP Code" plugin. Now I wanted to auto login the users when they complete the registration. I did lot of research but couldn't find a solution for my problem. I tried the following sample snippet to test in the custom page :

<?php

//Log in a WordPress user programmatically
function auto_login( $user ) {
    $username   = $user;
    if ( !is_user_logged_in() ) {
        $user = get_user_by('login', $username );
        $user_id = $user->ID;
        wp_set_current_user( $user_id, $user_login );
        wp_set_auth_cookie( $user_id );
        do_action( 'wp_login', $user_login );
    }     
}

auto_login('admin');

?>

But I am getting following errors.

Warning: Cannot modify header information - headers already sent by (output started at /home/content/19/11054119/html/wp-content/plugins/sitepress-multilingual-cms/inc/language-switcher.php:921) in /home/content/19/11054119/html/wp-includes/pluggable.php on line 925

Warning: Cannot modify header information - headers already sent by (output started at /home/content/19/11054119/html/wp-content/plugins/sitepress-multilingual-cms/inc/language-switcher.php:921) in /home/content/19/11054119/html/wp-includes/pluggable.php on line 926

Warning: Cannot modify header information - headers already sent by (output started at /home/content/19/11054119/html/wp-content/plugins/sitepress-multilingual-cms/inc/language-switcher.php:921) in /home/content/19/11054119/html/wp-includes/pluggable.php on line 927

So I am wondering is there anyway to do auto login without touching the theme functions. If you have any idea or solution help me with this..

Thanks

3

3 Answers

0
votes

You should use the core function wp_signon https://codex.wordpress.org/Function_Reference/wp_signon this is the sample code

function custom_login() {
    $creds = array();
    $creds['user_login'] = 'example';
    $creds['user_password'] = 'plaintextpw';
    $creds['remember'] = true;
    $user = wp_signon( $creds, false );
    if ( is_wp_error($user) )
        echo $user->get_error_message();
}
0
votes

You can try this way:

   if( isset($_POST['log']) && isset($_POST['pwd']) ):
      $creds = array( 'user_login' =>  $_POST['log'], 'user_password' => $_POST['pwd'], 'remember' => $_POST['rememberme'] );
      $user = wp_signon( $creds, false );
      if ( is_wp_error($user) ): echo $user->get_error_message(); endif;
      wp_set_current_user($user->ID);
      return $user;
    endif;

You could look for more info here

0
votes
 //Log in a WordPress user programmatically
        function auto_login( $username ) {            
            if ( !is_user_logged_in() ) {
                $UserData = get_user_by('login', $username );
                $iUserid = $UserData->ID;
                $UserLogin = $UserData->user_login;
                wp_set_current_user($iUserid, $UserLogin);
                wp_set_auth_cookie($iUserid);
                do_action('wp_login', $UserLogin);
            }     
        }

        auto_login('admin');