Search code examples
wordpresscookieshooksetcookieshortcode

Wordpress Shortcode setcookie Issue


No errors or warnings are generated when running the code below.

I just ran into a problem on wordpress when trying to create cookies inside of shortcode events.
In the code below, the test1 cookie is created just fine, however the test2 cookie is not created.
The verbiage 'Code Ran' does display however.

if ( ! function_exists('cookieset')) :
function cookieset( $atts, $content = null ) {
    extract( shortcode_atts( array(
        'name'     => 'cookieloc',
        'redirect' => '/',
        'expires'  => '365',
    ), $atts ) );
    setcookie("test2", 1, time()+3600, "/", ".domainname.com", false, true);
    return "Code Ran";
}
endif;

if ( ! function_exists('cookieloc_action') ) :

function cookieloc_action() {
    echo add_shortcode('cookieloc', 'cookieset');
    setcookie("test1", 1, time()+3600, "/", ".domainname.com", false, true);  
}
endif;

Solution

  • It appears the Word Press does not buffer the entire page content, but progressively sends the content, thus I cannot send headers in an add_shortcode(...) function

    Sadly, the order of execution is not provided in the API sections on wordpress... http://codex.wordpress.org/Shortcode_API