Categories
Uncategorized

How to automatically login a user into WordPress

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. The WorpPress function wp_singon() is the perfect solution.

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!

Leave a Reply

Your email address will not be published. Required fields are marked *