When developing for WordPress, sometimes you may need to create a PHP script that will automatically login a user so you can enable user functions. I needed to do something like that when creating a public form to submit posts from the front end using Ajax. The WorpPress function wp_singon() was the perfect solution.
wp_signon()
The wp_signon() function takes the user account’s username and password as parameters, and will allow you to set up a secure cookie for the new session. For more info, check out the function’s codex page: http://codex.wordpress.org/Function_Reference/wp_signon
Another way to auto-login:
Cleverwp.com has an interesting post on how to Autologin a WordPress user in your PHP script which only requires the user’s login name. It’s pretty straight forward, though not as secure.
3 replies on “How to automatically login a user into WordPress”
Hey, i tried using wo_signon with ajax but this script sends data to the document head which mean you would have a growing error file in your http://ftp... furthermore if you try to click and move to another page you will find you are not logged in…
Have you been facing the same issuse?
Hi Sagive,
Do you have some example of the code you’re trying to use? That way I can look at it and see what seems to be the problem.
In the meantime, wp_signon() returns either WP_User (object) or WP_Error (array), but it does not print them out. Please check your code to make sure you don’t have any extra spaces after or before your php blocks (), which is usually the reason PHP complains about output being printed after headers have already been sent. Thanks!
This does the job nicely:
http://wordpress.org/plugins/autologin-links/
Auto Login to WordPress site – maintains cookie and session (persistent)
* Do not try to use CURL to do this as that’s on the server side cannot pass cookies to the browser client.