0

I'm building a custom login form and I've tried two methods: wp_authenticate() and wp_signon().

For the former, I am able to correctly return the WP_User object upon a successful authentication, but no cookies are set to signal to is_user_logged_in() that the user is indeed logged in. I suppose I can set my own cookies but is there an easier way?

For the latter, I keep having the error Cannot modify header information - headers already sent by.... I've tried calling the wp_signon() function from the <head>, the actual header, and in the page template. I see the same error regardless. I've looked online but I'm not seeing any solutions to this exact problem.

The specific error is pointing to line 7 of base.php - Warning: Cannot modify header information - headers already sent by (output started at /srv/www/mysite/current/web/wp/wp-content/themes/mysite/base.php:7).

But looking right there, I don't see any issue (these are lines 4 - 10, line 7 is the doctype):

use Roots\Sage\Wrapper;

?>
<!doctype html>
<html <?php language_attributes(); ?>>
  <?php get_template_part('templates/head'); ?>
  <body <?php body_class(); ?>>

As per WP's own troubleshooting, I've checked that there are no extra spaces.

Regardless of how it's gone about, I just need to be able to log a user in with their credentials and have the ability to check their logged-in status to create a logged-in state on a website.

Any help?

1
  • 1
    It might be easier to style the existing login form than to build a custom one. That way you don't have to keep up with a bunch of different Core functions, just some CSS.
    – WebElaine
    Nov 21, 2017 at 19:12

1 Answer 1

0

Look at the authenticate filter. This will handle everything for you.

function wpse_custom_login($user, $username, $password) {
    if (empty($username)) {
        $errors = new WP_Error();
        //add some error message, return $errors
    }
    $user = get_user_by('email', $username);
    // do some additional validation, checks, etc.
    return $user;
}
add_filter('authenticate', 'wpse_custom_login', 30, 3);

This is a really barebones example but begins to demonstrate how you would log in a user by email address instead of username.

Your Answer

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

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