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.

As the title suggests, I'm building a custom front-end login page for users of my site. I'm using wp_signon to log the user in after their credentials have been validated, but I'm getting this error:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\wordpress\wp-content\themes\twentyeleven\header.php:13) in C:\xampp\htdocs\wordpress\wp-includes\pluggable.php on line 737

twentyeleven\header.php:13 shows this (lines above and below included):

<!--[if IE 6]>
<html id="ie6" <?php language_attribute(); ?>>
<![endif]-->

I've read a thing or two that said wp_signon must occur before any page content is created because it alters the headers, but I guess I'm not really sure how I'd use it then. Where should wp_signon go? Here's the structure of my code as it is:

//Retrieve submitted $_POST values.
//Check if this is the first time the form has been displayed (there is a hidden input at the end of the form that changes a boolean to false for all future submits)
//If it's not the first time, do form validation and create an array with error ID's to be displayed in the form. One of these possible errors is wrong username/password combo:
if (!get_userdatabylogin( $submitted['email'] ))
     $errors['emailIsValid'] = false;
else
{
     $user_data = get_userdatabylogin( $submitted['email'] );
     $password_hashed = $user_data->user_pass;
     if (wp_check_password($submitted['password'], $password_hashed, $user_data->ID))
     {
          $credentials = array();
          $credentials['user_login'] = $submitted['email'];
          $credentials['user_password'] = $submitted['password'];
          if (empty($submitted['rememberme']))
               $credentials['remember'] = false;
          else
               $credentials['remember'] = true;
          wp_signon($credentials, true);
     }
     else
          errors['passwordMatchesEmail'] = false;
}
//Check to see if the errors array has any flags, if so flag a variable to say errors exist
//Form code: form submits to itself
//Check error flags and display error message accordingly
//Display body of form

Everything works perfectly except for that wp_signon line. Entering the correct credentials yields the error I mentioned above.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You have to hook earlier in the request to log someone in, you can't do it from a page template because, as the error says, headers have been sent to the browser already, so it's too late at that point.

<?php   
add_action('template_redirect', 'my_check_login');

function my_check_login(){
    if( is_page('my-login-page') && isset($_POST['email']) ){
        // check input and
        // do my login stuff here   
    }
}
share|improve this answer
    
Thanks! That worked perfectly. Now I just have to figure out how to get wp_redirect() to work... –  daniechiu Aug 25 '11 at 20:11
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.