Leave a Reply

1 comment

  1. You can log a user in securely by simply calling wp_signon with the proper credentials. This has to be done before any output is produced, so that WP can set the users cookie properly.

    Example:

    $user = wp_signon(array('user_login'=>'example', 'user_password' = > 'swordfish'));

    That will do a proper login from the front end of the site, set cookies, and return the user object. Then you can set the $commentdata[‘user_id’] = $user->ID and return, and the comment will be dealt with accordingly.

    An auth failure will make that return a WP_Error object instead, so use is_wp_error on the result to see if it’s a WP_Error or a WP_User object that you get back.