12

I've been trying for days now to take users who have just registered to my WordPress site and automatically log them in and then redirect them to a URL of my choice.

By default, WordPress sends you a username and a password, then you must log in manually. This is a total pain. How can i overcome this.

I have my own registration page(core php page) which successfully adds users into DB. But the point is, i should avoid users to login again.

Once registration is done, it should automatically redirects to home page or profile page.

I am a newbie to wordpress functionalities. It would be grateful if someone(who have knowledge on core functionality of wordpress) at least suggests a way/solution.

Looking forward.
Thanks

Ruvee
  • 8,611
  • 4
  • 18
  • 44
smallerik
  • 175
  • 1
  • 3
  • 11

4 Answers4

37

// Add on function.php for Auto login after register and redirect to a home page. varified this code

function auto_login_new_user( $user_id ) {
    wp_set_current_user($user_id);
    wp_set_auth_cookie($user_id);
    $user = get_user_by( 'id', $user_id );
    do_action( 'wp_login', $user->user_login );//`[Codex Ref.][1]
    wp_redirect( home_url() ); // You can change home_url() to the specific URL,such as "wp_redirect( 'http://www.wpcoke.com' )";
    exit;
}
add_action( 'user_register', 'auto_login_new_user' );
Kamran Syed
  • 439
  • 3
  • 7
Ravi Patel
  • 5,121
  • 2
  • 25
  • 44
  • 1
    Hi Patel..Thanks for your code..but i tried to login with sign on function..but it throws an error like **ERROR: The password field is empty** pls check my following code..`$getdetails= mysql_fetch_array(mysql_query("SELECT * FROM `wp_users` WHERE `ID`='$user_id'")); $username=$getdetails['user_login']; $pass=$getdetails['user_pass']; $creds = array(); $creds['user_login'] = $username; $creds['user_pass'] = $pass; $creds['remember'] = true; $user = wp_signon( $creds, false ); if ( is_wp_error($user) ) echo $user->get_error_message(); add_action( 'user_register', 'auto_login_new_user' );` – smallerik Nov 15 '13 at 10:40
  • this a default wordpress function so not pass any value in function only add on function file. or you can use any plugin for registration? – Ravi Patel Nov 15 '13 at 12:35
  • 1
    Just like the add_action hook below, this stopped sending new WP user notifications. A dangerous function. I had to send my mail from within the function in order to get it sending again. Because it doesn't use the typical "wp_new_user_notification" hook, it was also hard to track down when the bug came up. Unless you have a plan for the mail within this function --- I'd recommend staying away from this one. – Sean Halls Aug 06 '17 at 17:35
  • @SeanHalls : Please check updated code which on edited by kamran. – Ravi Patel Oct 30 '18 at 14:53
10

Following is based on how WooCommerce creates a new user and logs him in:

$user_pass = esc_attr( $_POST['account_password'] );

$new_user_data = array(
    'user_login' => $_POST['account_username'],
    'user_pass'  => $user_pass,
    'user_email' => $_POST['account_email'],
    'role'       => 'subscriber'
);

$user_id = wp_insert_user( $new_user_data );

// Set the global user object
$current_user = get_user_by( 'id', $user_id );

// set the WP login cookie
$secure_cookie = is_ssl() ? true : false;
wp_set_auth_cookie( $user_id, true, $secure_cookie );

to redirect use wp_safe_redirect, e.g.

wp_safe_redirect( home_url( '/' ) );
exit;
diggy
  • 6,748
  • 2
  • 18
  • 24
  • Hi diggy..thanks for the code..but i tried in different way using signon() function..but it throws an error like **ERROR: The password field is empty** though password is generating..i hope its due to cookie problem..pls verify my below code and help me out.._$getdetails= mysql_fetch_array(mysql_query("SELECT * FROM `wp_users` WHERE `ID`='$user_id'")); $username=$getdetails['user_login']; $pass=$getdetails['user_pass']; $creds = array(); $creds['user_login'] = $username; $creds['user_pass'] = $pass; $creds['remember'] = true; $user = wp_signon( $creds, false ); – smallerik Nov 15 '13 at 10:45
0

Instead of touching core files... You can use this

$secure_cookie = is_ssl();
$secure_cookie = apply_filters('secure_signon_cookie', $secure_cookie, array());
global $auth_secure_cookie;
$auth_secure_cookie = $secure_cookie;

wp_set_auth_cookie($user_id, true, $secure_cookie);
$user_info = get_userdata($user_id);
do_action('wp_login', $user_info->user_login, $user_info);

The function wp_create_user returns the just created user_id which you can use to create a cookie and log the user in. If you wish, you can redirect the logged in user to the profile or home page.

-6

Thanks for your support guys..i did on my own with the following code..thanks for your time and support :)

<i>$getdetails= mysql_fetch_array(mysql_query("SELECT * FROM `wp_users` WHERE `ID`='$user_id'"));
$username=$getdetails['user_login'];


$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = true;

    $user = wp_signon( $creds, false );
    if ( is_wp_error($user) ){
        echo $user->get_error_message();
    }else{
        wp_redirect( home_url() );
    }
hemnath mouli
  • 2,617
  • 2
  • 17
  • 35
smallerik
  • 175
  • 1
  • 3
  • 11