Reputation: 7839
I'm having a little trouble setting a cookie in my WordPress installation.
Here is my code:
function write_cookies() {
setcookie("category", get_query_var('cat'), time() + 3600 * 24, COOKIEPATH, '', false);
print($_COOKIE["category"]);
}
add_action('init', 'write_cookies');
In theory, this should output the categoryId
, but nothing seems to be working.
I read that on localhost, providing a blank string as the cookie domain works, but I can't get any kind of output. I've also tried 'localhost'
, '.localhost'
, '127.0.0.1'
, false
and NULL
as the domain. The COOKIEPATH
constant currently outputs '/'
I'm reading the cookies through Chrome, and there doesn't seem to be any change in the cookies on the page.
UPDATE:
Thanks for the answers so far, I'll explain why I need cookies in this instance...
Basically, on my post pages, I wish to show other posts in the same category. For those with multiple categories that means storing which route the user took to reach the post. I'll give a real world example:
A user will navigate from Home, to Industry News to post entitled New Funding for Manufacturing Sector Reinvigorates Local Politicians. Now, in the Other Posts for this section, I would like to show Industry News posts, but as the particular post in question is set up under both Industry News and Political News, how do I tell which route was taken?
In the first instance, this is set up to analyse wp_get_referer() to determine which category came before the post. However, if I then use this Other Posts section to move to a post entitled New Regional Developments in the Manufacturing Sector, which is classed as both Industry News and Regional News, the wp_get_referer returns the previous post, not the category as required in my logic.
My solution was to (upon visiting a category page) store the id of the category and use this in all future logic to determine the correct category to show in the Other Posts section.
Confusing, I know, but I'm limited in both design and client needs to deliver this particular flow.
As suggested, I've updated my code to display:
function write_cookies() {
setcookie("category", get_query_var('cat'), false, '/', str_replace('http://www','',get_bloginfo('url')));
print('test: '.$_COOKIE["category"]);
}
add_action('init', 'write_cookies');
This outputs test:
without the cookie being printed.
Upvotes: 0
Views: 1831
Reputation: 7839
I tested a variety of Cookie Domains on a non-WordPress installation and I found that in a localhost environment, the only domain that was successful in setting the variable was ''
i.e. an empty string.
Here is my code now:
function write_cookies() {
setcookie("category", get_query_var('cat'), false, '/', '',false);
}
I've also adjusted it so it's called from within the post loop and not from the 'init' event, this gives me access to a considerable degree more variables to set into the cookie than on the init event.
Upvotes: 0
Reputation: 4060
There is a known limitation of Chrome
http://code.google.com/p/chromium/issues/detail?id=56211
You can't set/access http://localhost cookies. Odd issue in Chrome.
Upvotes: 1
Reputation: 2093
I would let wordpress figure out the proper domain for me using something like: str_replace('http://www','',get_bloginfo('url'))
. That is if wordpress default categories wouldn't suffice.
Generally speaking, using Wordpress default category and tag structures is far preferable to raw cookies. So why again was it you need a cookie?
Upvotes: 1