本文介绍了主题开始渲染后无法在Wordpress插件中设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在Wordpress插件中设置cookie时,出现已发送标题"错误.有人可以建议我如何解决此问题吗?
When I set a cookie in a Wordpress plugin, I get the "headers already sent" error. Can someone suggest how I can fix this?
我的代码:-
if(isset($_REQUEST['id']) && !is_user_logged_in())
{
require_once( ABSPATH . WPINC . '/registration.php' );
$registration = get_option( 'users_can_register' );
global $wpdb;
$user_id = $wpdb->get_var( $wpdb->prepare("SELECT user_id FROM
$wpdb->usermeta WHERE
meta_key = 'id' AND meta_value =%s",$id) );
if ( empty($user_id) )
{
$wp_user_obj = get_user_by('email', $email);
$user_id = $wp_user_obj->ID;
}
if ( $user_id)
{
wp_set_auth_cookie( $user_id );
wp_set_current_user( $user_id );
if ( isset( $_REQUEST['redirect_to'] ) && !empty(
$_REQUEST['redirect_to'] ) )
{
wp_redirect( home_url() );
}
else
{
wp_redirect(home_url() );
}
}
else
{
$userdata = array();
$user_id = wp_insert_user($userdata );
wp_new_user_notification($user_id,$user_pass);
if ( $user_id )
{
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $user_pass;
if ( !empty( $remember ) )
{
$creds['remember'] = true;
}
$user = wp_signon( $creds, true );
update_usermeta( $user_id,'id',esc_attr( $_REQUEST['id']));
update_usermeta( $user_id,'fname',esc_attr( $_REQUEST['fname']));
update_usermeta( $user_id,'lname',$_REQUEST['lname']);
update_usermeta( $user_id, 'email',esc_attr( $_REQUEST['email']) );
wp_redirect(home_url() );
}
}
}
global $user_ID; $user = get_userdata( $user_ID );
if(is_user_logged_in())
{
echo $user->user_login ;
}
and heder.php line:- `<html <?php language_attributes(); ?>>`
推荐答案
您调用set_cookies
函数为时已晚.在您调用它时,输出已经开始.您需要在开始任何输出之前调用它,因为如果已经开始输出,则无法设置cookie(另请参见: setcookie
).
You're calling your set_cookies
function too late. At the time you call it, output has already started. You need call it before any output has started, because cookies can not be set if output has already started (see as well: setcookie
).
根据您的情况,在执行header.php
第11行之前.
In your case before header.php
line 11 is being executed.
那仅仅是前的主题GET的已加载.
这篇关于主题开始渲染后无法在Wordpress插件中设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!