Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm manually creating users programmatically, and I want to sign in the newly created user. WP makes it easy to access the hashed password, but not the plaintext version. Is there a way to use wp_signon() without the plaintext password?

I found one person who claims to have done this here, but it didn't work for me.

THANKS!

share|improve this question
    
I think you can just assign the user object of the user you just created to the current_user global variable –  onetrickpony May 28 '12 at 13:27
add comment

2 Answers

up vote 6 down vote accepted

wp_set_auth_cookie() will log a user in without having to know their password.

share|improve this answer
    
This worked great. However, when I use it, the conditional is_user_logged_in() doesn't seem to work. Do you know if it's looking at something different than the cookies? –  SDP May 28 '12 at 13:44
    
@Emerson - what hook are you logging them in on? it has to be before headers are sent. also try to wp_set_current_user before logging them in. –  Milo May 28 '12 at 13:47
    
I actually wasn't calling it from a hook at all. I just added wp_set_auth_cookie() into my signin function. I guess I need to rethink that. I'll also lookup wp_set_current_user and report back. Thank you very much for your help on this! –  SDP May 28 '12 at 13:59
    
Well, is it possible to login a user without having his details exist in database? Just setting few cookies in browser through script is enough? Please let me know. –  shasi Feb 6 at 9:10
add comment

The following code does the job for automatic login, without any password!

// Automatic login //
$username = "Admin";
$user = get_user_by('login', $username );

// Redirect URL //
if ( !is_wp_error( $user ) )
{
    wp_clear_auth_cookie();
    wp_set_current_user ( $user->ID );
    wp_set_auth_cookie  ( $user->ID );

    $redirect_to = user_admin_url();
    wp_safe_redirect( $redirect_to );
    exit();
}
share|improve this answer
    
Well, it works great. Just the username is enough, which is case insensitive. –  shasi Feb 6 at 7:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.