0

i am writing a plugin, where i am setting a cookie. When i set cookie using php method it show warning.

Warning: Cannot modify header information - headers already sent by (output started at

if i use wordpress action, for setting cookie its not working. I have use this in my theme functions.php file its working fine but not working in plugin code.

Here are code samples:

Using php method

setcookie( 'sm_js_cookie', 1, time()+3600*24*100,'/');

Using wordpres action:

function set_newuser_cookie() {

    setcookie( 'sm_js_cookie', $_SERVER['REMOTE_ADDR'], time()+3600*24*100,'/');

}

add_action( 'init', 'set_newuser_cookie');

Please help me to find its solution.

  • Can you please delete your cache and cookies and try it again? – cgee Nov 12 '15 at 12:50
  • @cgee can you please tell me , from where i can delete cache and cookies. – Muhammad Saqlain Arif Nov 12 '15 at 12:54
  • Delete your cache and cookies from your browser.. You can google it.. "firefox delete cookies" or something. – cgee Nov 12 '15 at 12:56
  • Do you happen to output characters(even white space) in your plugin? View the source of your page and see if there are empty lines before the ` – Nikola Ivanov Nikolov Nov 12 '15 at 13:02
  • @cgee not working , after clear cache and cookies. – Muhammad Saqlain Arif Nov 12 '15 at 13:09
  • @Nikola Ivanov Nikolov I have tried all these, there is no extra space. – Muhammad Saqlain Arif Nov 12 '15 at 13:10
  • Can you please try to replace this add_action "add_action( 'init', 'set_newuser_cookie');" above of your function? – cgee Nov 12 '15 at 13:13
  • @cgee tried by not working.:( i looking for solution from last 48 hours. :( – Muhammad Saqlain Arif Nov 12 '15 at 13:16
  • Can you try to create a copy of your functions.php file(where the code worked), then copy all of the code from your plugin file and paste it in the copy of functions.php. Delete the plugin file, move the copy of functions.php and rename it to match the deleted file. I've had situations where there is no visible whitespace, but something is messed-up with the file itself. – Nikola Ivanov Nikolov Nov 12 '15 at 13:17
  • @Nikola Ivanov Nikolov tried this out , but no succes :( – Muhammad Saqlain Arif Nov 12 '15 at 13:25
  • Ok, can you find the minimal amount of code that you need in order to reproduce the issue? Remove all code, besides the one that sets the cookie on init. See if you have the issue. If not, add back some code. Repeat until you get the same issue. Then upload the code somewhere(gist.github.com or similar) and share the link. Until then, we can only guess where the problem is. – Nikola Ivanov Nikolov Nov 12 '15 at 14:16
  • @Nikola Ivanov Nikolov right now the thing which i have test if write code for creating cookie outside my method its working fine. i will write in my method like below and call short code it not works. :( function sm_excute_scripts(){ } add_shortcode('js_conditions_script','sm_excute_scripts'); – Muhammad Saqlain Arif Nov 12 '15 at 16:14

1 Answers1

2

After going back and forth in comments, it turns out that you're trying to call setcookie from within a shortcode(you should have mentioned that in your question).

This will usually not work, because by the time that a shortcode is parsed, some output has been sent to the browser, and therefore you can't use the setcookie function.

Your alternative as you've already figured it out is to use setcookie from within a callback function on the init action. However you can usually hook all the way up to template_redirect(I believe this is the last, or one of the last actions you can use for sending headers). Can you specify what you're trying to achieve so that I can give you appropriate suggestions?

Without any additional details, I can give you the below example on how to set the cookie based on whether the shortcode is present on the page/post the user is currently viewing.

<?php
function set_newuser_cookie(){
    // Check to see if we're looking at a single post, page or attachment
    if ( is_singular() ) {
        $_post = get_post();
        // See if the content of the current post object has the shortcode
        if ( has_shortcode( $_post->post_content, 'js_conditions_script' ) ) {
            setcookie( 'sm_js_cookie', 1, time()+3600*24*100,'/');
        }
    }
}
add_action( 'template_redirect', 'set_newuser_cookie' );

What we do is that we hook to the template_redirect action(the main WordPress query has been executed so we know what page we are looking at). We check to make sure we are looking at a single post(including custom post types), page or attachment and then we check to see if it contains the shortcode [js_conditions_script]. If it does, we set the cookie.

Nikola Ivanov Nikolov
  • 5,022
  • 1
  • 26
  • 27