0

How does one set cookies in WordPress without getting the ‘headers not sent’ error? within WP, I can’t write a cookie that parses a shortcode without getting said error. Please note the snippet below is part of a larger plugin code.

add_shortcode('show_stats', 'settings');
function settings(){
if(!isset($_COOKIE['site_stats']))
{
    // set a cookie that will expire in 5 minutes (date formats: 'years', 'months', 'days', 'hours', 'minutes')
    $counter_cookie = setcookie('kobopulse_site_stats', time(), 5* MINUTE_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); 
    }
    else
    {
        $counter_cookie = $_COOKIE['site_stats']; 
    }
}

According to this question here, Searching hook to set cookies before <head> and avoid "headers already sent", hooking to init does the trick, but within the context of this above and inside a plugin, how do I hook to init in addition to the add_shortcode function i have already used?

  • 1
    Why are you trying to set cookies inside post content? Setting cookies involves setting headers, but the moment you output anything PHP automatically generates and sends a set of headers. It's the equivalent of giving your order at a coffee shop, you can't start modifying your order halfway through them making the drink without throwing it away and starting again. Instead explain what problem this is trying to solve, you wouldn't set a cookie in a shortcode, and it isn't the appropriate thing to do – Tom J Nowell Jul 16 '16 at 22:15
  • @TomJNowell; This code snippet is part of a Site Stats plugin. The cookie is meant to track a user as he visits the site. This cookie data is stored in the database and deleted after a set time of inactivity. The cookie data is used in an insert and update query in the plugin. I have the option of tracking , using visitor IP, but then the IP is not a good option as more than a thousand users may be on a shared IP. I shall eventually store a user specific data in the cookie. If I can not use a cookie tracking system in the plugin, then what viable tracking option do I have? – Terungwa Jul 17 '16 at 6:38
  • shortcodes are for adding content, not for setting cookies. what you are trying to do is just wrong, you have two different functionalities that don't have any reason to live together in the same piece of code – Mark Kaplun Jul 17 '16 at 9:13
  • Also, any plugin that tracks visitors inside WordPress is going to run into race conditions and other problems, as well as significantly slow down your site. You shouldn't be writing to the database on the frontend, but it's unavoidable with those kinds of plugins as they need to store the visit. Instead, talk to a 3rd party service such as google analytics and cache the data, update it every few hours – Tom J Nowell Jul 17 '16 at 11:30
  • @MarkKaplun; the shortcode does not set the cookie, perhaps I have not clearly expressed the problem by not posting the full code. The primary problem is to set cookies in WordPress without getting the ‘headers not sent’ error. The answer seem to be quite simple and straight forward: setting a function that’s hooked into init hook and using the cookie value in my code elsewhere. That is all I ended up doing. Kindly take a look at the solution I have pasted. – Terungwa Jul 17 '16 at 12:54
0

The solution is setting a function that’s hooked into init.

add_action('init', 'stats_cookie');
function stats_cookie()
{
    if(!isset($_COOKIE['site_stats']))
    { 
        $current = current_time( 'timestamp', 1);
        setcookie('site_stats', $current, time()+60+60*24*7, COOKIEPATH, COOKIE_DOMAIN ); 
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.