Site icon Hip-Hop Website Design and Development

How does one set cookies in WordPress without getting the ‘headers not sent’ error? within WP

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?