-1

How can I set a cookie value which getting from the shortcode attributes .?

For example , in this shortcode [shortcode ids="12"][/shortcode] , I need to set these ids in a cookie. When I tried to use setcookie ('ids', '12', time()+31536000, '/', 'localhost', '0'); I get the warning message

Warning: Cannot modify header information - headers already sent by (output started at /var/www/wordpress_retail/wp-content/themes/twentythirteen/header.php:13).

What could be the issue?

I used this code inside the function custom_feilds add_shortcode('shortocode', array(&$this, 'custom_feilds'));

mathielo
  • 6,725
  • 7
  • 50
  • 63
sarath
  • 698
  • 6
  • 20
  • 1
    possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – ChrisGPT was on strike Jan 13 '15 at 13:19

1 Answers1

0

You must set cookies before any output is sent to the browser. You need to use some of the early running hooks which you can set via a plugin or theme file (functions.php for example).

Something like

add_action('init', function() {
    if (!isset($_COOKIE['ids'])) {
        setcookie('ids', '12', time()+31536000, '/', 'localhost', '0');
    }
});
daker
  • 3,430
  • 3
  • 41
  • 55
  • Hi daker , thanks for the reply .As you mentioned the we can set the the cookie in the 'init' function. but the problem is how we can get the value of ids (here the value is '12' ),at that time of init. because this value is in the [shortcode ids="12"][/shortcode] which we get on the time of getting page content. ? – sarath Jan 14 '15 at 05:32
  • @sarath have a look at http://codex.wordpress.org/Shortcode_API - maybe you can save the cookie at shortcode init? What are you trying to do? – daker Jan 14 '15 at 08:30