0

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.

1 Answer 1

2

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   
    }
}
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.