2013-06-11 244 views
0

您好我对wordpress,php和所有这些编辑工具都很陌生。我想在用名称“xxx”和值“(currentusername)”进行身份验证时向wordpress添加一个新的cookie。我已经阅读http://wptheming.com/2011/04/set-a-cookie-in-wordpress/。我将所需的代码添加到我的代码的functions.php中,但我不知道如何调用它,以便将当前用户名登录添加到cookie中。 在此先感谢将自定义Cookie添加到Wordpress

下面是我在我的functions.php插入

function set_newuser_cookie() { 
if (!isset($_COOKIE['sitename_newvisitor'])) { 
    setcookie('sitename_newvisitor', 1, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false); 
} 

} ADD_ACTION( '初始化', 'set_newuser_cookie')其他网站上的代码;

回答

0

碰到这一个 - 我建议不要添加一个新的cookie,而是我会劫持(利用)当前的cookie,让WP为你管理它。此外,在WP可用的钩子允许非常干净和严格的代码中使用WP的功能 - 试试下面的代码片段 - 我把意见和试图要详细:

function custom_set_newuser_cookie() { 
    // re: http://codex.wordpress.org/Function_Reference/get_currentuserinfo 
    if(!isset($_COOKIE)){ // cookie should be set, make sure 
     return false; 
    } 
    global $current_user; // gain scope 
    get_currentuserinfo(); // get info on the user 
    if (!$current_user->user_login){ // validate 
     return false; 
    } 
    setcookie('sitename_newvisitor', $current_user->user_login, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false); // change as needed 
} 
// http://codex.wordpress.org/Plugin_API/Action_Reference/wp_login 
add_action('wp_login', 'custom_set_newuser_cookie'); // will trigger on login w/creation of auth cookie 
/** 
To print this out 
if (isset($_COOKIE['sitename_newvisitor'])) echo 'Hello '.$_COOKIE['sitename_newvisitor'].', how are you?'; 
*/ 

是的,使用的functions.php此代码。祝你好运。

相关问题