2015-05-03 33 views
0

當我嘗試這個代碼不能使用wp_signon()

$creds = array('user_login' => $_POST['benutzername'], 'user_password' => $_POST['passwort'], 'remember' => true); 
$user = wp_signon($creds, false); 
if (is_wp_error($user)): echo $user->get_error_message(); endif; 
wp_set_current_user($user->ID); 
return $user; 

我得到這個消息:

Warning: Cannot modify header information - headers already sent by (output started at /home/httpd/vhosts/your-click.ch/httpdocs/wp-includes/formatting.php:4179) in /home/httpd/vhosts/your-click.ch/httpdocs/wp-includes/pluggable.php on line 925 Warning: Cannot modify header information - headers already sent by (output started at /home/httpd/vhosts/your-click.ch/httpdocs/wp-includes/formatting.php:4179) in /home/httpd/vhosts/your-click.ch/httpdocs/wp-includes/pluggable.php on line 926 Warning: Cannot modify header information - headers already sent by (output started at /home/httpd/vhosts/your-click.ch/httpdocs/wp-includes/formatting.php:4179) in /home/httpd/vhosts/your-click.ch/httpdocs/wp-includes/pluggable.php on line 927 Catchable fatal error: Object of class WP_User could not be converted to string in /home/httpd/vhosts/your-click.ch/httpdocs/wp-includes/shortcodes.php on line 286

我正在使用shortcode.php代碼。我的代碼有什麼問題?

回答

2

按照WP手冊爲wp_signon功能:

This function sends headers to the page. It must be run before any content is returned.

根據你的錯誤,看來你使用此功能的一些內容已經被退回了。

進一步,似乎WP團隊建議使用該功能在鉤after_setup_theme其中:

will make it run before the headers and cookies are sent, so it can set the needed cookie for login.

+0

謝謝你,成功了! –

+0

不客氣。 –