Using wordpress login functions

21,088

Solution 1

You need to change this line:

wp_set_auth_cookie( $user, 0, 0);

To this:

wp_set_auth_cookie( $user->ID, 0, 0);

$user is a WP_User Object not user id.

wp_signon returns WP_Error on failure, or WP_User on success.

Solution 2

Was having real trouble myself... finally got it to work..!! (after several days of experimenting and banging my head against it)

One thing to make sure is that you haven't sent any output yet or the session cookie won't write as it needs to be in the header. Also if you call wp_signon before the session starts, there session info gets lost also... sheesh... strange but I had both happening to me. Anyhoo no more ado...

// Create new user (for example)

$userdata = array('user_login'->$username,'user_pass'->$password);
$user_id = wp_insert_user($userdata);

// Make sure username is up to date... I needed this as there was a hook to user_register being called in wp_insert_user but that hook is called after the user is created, so it needed a DB cache clear to work otherwise the wrong username was set... So the user was auto-logged in for one page only - ridiculous. (It was Register Plus Redux doing the hooking btw.)

wp_cache_delete($user_id, 'users');
wp_cache_delete($username, 'userlogins');
$userdata = get_userdata($user_id);
$username = $userdata->user_login;

// Make sure user session has started

$vsessionid = session_id();
if (empty($vsessionid)) {session_name('PHPSESSID'); session_start();}

// Login Current User

wp_clear_auth_cookie();
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon($creds, false);

// Check it worked

if (is_wp_error($user)) {$error = $user->get_error_message();}
else {
    wp_set_current_user($user_id);
    // The next line *really* seemed to help!
    do_action('set_current_user');
    $current_user = wp_get_current_user();
    if (is_wp_error($current_user)) {$error = $user->get_error_message();}
}

if ($error) {echo $error; print_r($userdata); print_r($current_user);}

Solution 3

current_user();

function current_user()
{
    global $current_user,$user_ID;

    if(is_user_logged_in())
    {
        echo 'User Logged in '.$user_ID;
    }
    else {
        echo 'No user is logged in< br/>';
        custom_login();
    }
}

function custom_login() {
    $creds = array('user_login' => '<USERNAME>', 'user_password' =>   '<USERPASSWORD>', 'remember' => true );
    $user = wp_signon( $creds, false );
    wp_set_current_user($user->ID); 
    wp_set_auth_cookie($user->ID, true, false );    
    do_action( 'wp_login', '<USERNAME>' );

    if ( is_wp_error($user) )
        echo $user->get_error_message();
    }
}
Share:
21,088
Sbad
Author by

Sbad

Updated on June 03, 2020

Comments

  • Sbad
    Sbad almost 4 years

    Does anybody have any experience writing a custom Wordpress login page using the functions:

    wp_signon()
    and wp_set_auth_cookie()
    

    found on http://codex.wordpress.org/Function_Reference/

    I can't seem to get them working.

    The code looks something like this:

    function login_wordpress($username, $password) {
        $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();
           die();
        } else {
             wp_set_auth_cookie( $user, 0, 0);
        }
    }
    

    Am I missing something basic?

  • Sbad
    Sbad over 13 years
    No error message - nothing happens. wp_setcookie is deprecated.