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!
wp_set_auth_cookie()
will log a user in without having to know their password.The following code does the job for automatic login, without any password!
I have found another solution here that uses a better approach (at least in my opinion…).
No need to set any cookie, it uses the WordPress API:
I think the code is self explanatory:
The filter searches for the WP_User object for the given username and returns it.
A call to the function
wp_set_current_user
with the WP_User object returned bywp_signon
, a check with the functionis_user_logged_in
to make sure your are logged in, and that’s it!A nice and clean piece of code in my opinion!
This works well for me:
In addition to Mike, Paul and Sjoerd:
To better handle
login.php
redirections:To be placed in
wp-config.php
just afterFYI
Based on the above solution, I have released a plugin to keep the user logged in from one wordpress to another by synchronizing user data and cookie session:
https://wordpress.org/plugins/user-session-synchronizer/
Strange enough but the only way it works for me is if I redirect and die() after: