Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress wp_signon function not working

Tags:

wordpress

I am using wp_signon() function to login the user. I am doing this like

$creds = array();

$creds['user_login'] = $username;

$creds['user_password'] = $password;

$creds['remember'] = true;

$user = wp_signon( $creds, false );

i want to send user to home page after login.

But i Am facing following error:

Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\wpmoodle\wp-content\themes\twentyten\header.php:12) in E:\xampp\htdocs\wpmoodle\wp-includes\pluggable.php on line 690.

Thanks in advance.

like image 649
Dinesh Chandra Avatar asked Mar 17 '11 06:03

Dinesh Chandra


1 Answers

wp_signon() needs to run before you've sent any of your actual page to the browser.

This is because part of what wp_signon() does is to set your authentication cookies. It does this by outputting a "Set-Cookie: ..." header -- if you look at line 690 of pluggable.php, where your error comes from, you'll see that that line sets a cookie.

So, because wp_signon() outputs headers, you can't already have sent any content -- because headers must always be output before content.

However, the error indicates that you've already sent some output -- on line 12 of header.php, presumably some of the first HTML of the standard WordPress theme.

This basically indicates that you need to move your wp_signon() call to somewhere earlier in the WordPress processing, so it has a chance to output its headers before any page content is sent.

like image 151
Matt Gibson Avatar answered Nov 03 '22 04:11

Matt Gibson