Cookie is Empty in Wordpress Add Action with PHP
I am trying to access a cookie within a Wordpress action. The action registration is below and it works with static data. But when I try to access a cookie, a cooki开发者_Python百科e I know works because I have echoed it on the page, is always empty. Does this action have access to the $_COOKIE object? If not what solution can I do?
add_action ('comment_post', array(&$cisco_rewards, 'add_comment_badge_id'), 1);
function add_comment_badge_id($comment_id) {
add_comment_meta($comment_id, 'badge_id', $_COOKIE[Cisco_Rewards::REWARDS_COOKIE_NAME], true); // I put just a 2 here and it worked correctly, blank otherwise with the cookie.
}
$_COOKIE
is a superglobal variable so it is available inside any object (or any scope), so that can't be the problem. A couple of thoughts:
Have you tried to print $_COOKIE
just before you pass it to add_comment_meta()
so you can check the keys and values? Like:
function add_comment_badge_id($comment_id)
{
exit(print_r($_COOKIE));
add_comment_meta($comment_id, 'badge_id', $_COOKIE[Cisco_Rewards::REWARDS_COOKIE_NAME], true);
}
Check that you are setting the cookie with the same domain name with which you are doing the tests. Perhaps you are setting it with www.domain.com and you are accessing the page with http://domain.com ?
Note as well that you set the cookie on a request and it is available in the next one (see this brief but well explained answer here in SO).
精彩评论