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 cookie 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.
}
$_COOKIEis 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
$_COOKIEjust before you pass it toadd_comment_meta()so you can check the keys and values? Like: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 http://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).