I have setup some conditional logic within a WP theme to display certain sections based a cookie value.

I am trying now add some logic to override that cookie value in the request itself, based on a URL parameter.

The point being, that if the cookie exists, the normal conditional logic follows, but if the URL parameter exists, then the cookie should be changed BEFORE WP processes anything (but also ideally, before the server processes it*)

Q1: I currently have a working solution for WP, but I think it is using some bad practice (directly modifying superglobals) and I'd like to know if there is a better approach.

Q2: I would also like to know if there is a way to catch the request much earlier, like perhaps some way to do this in htaccess, so that LS Cache can serve the correct cached version based on a cookie cache vary..... I am using Litespeed and will setup a vary cache based on the cookie, but I dont think this will work with my current solution as I'm only changing the cookie once it gets to WP - I believe Litespeed would have already served the cached version.

This is what I'm currently doing - pretty simple.

add_filter('parse_request', 'maybe_set_dynamic_state_cookie');

function maybe_set_dynamic_state_cookie() {

    if (isset($_GET['state'])) {

        $valid_states = array('QLD', 'NSW', 'VIC', 'SA', 'WA', 'NT', 'ACT', 'TAS');

        $state = strtoupper( $_GET['state'] );

        if(in_array($state, $valid_states) ) {
            $_COOKIE['dynamicDataState'] = $state;

So the server may receive for example the following request:

:method: GET
:path: /students/?state=vic
:scheme: https
cookie: dynamicDataState=NSW

where URL parameter is different from submitted cookie. In this case, before anything happens it should be updated to:

:method: GET
:path: /students/?state=vic
:scheme: https
cookie: dynamicDataState=VIC

This is what currently occurs with the above function, but only once WP gets to it... I think LSChache would have already served the dynamicDataState=NSW cached version.

It is of less importance to the question here, but I am also doing the following to update the response headers so the users browser is updated to match the modified request change.

add_action( 'init', 'set_dynamic_state_cookie' );

function set_dynamic_state_cookie() {
    if (isset($_GET['state'])) {

        $valid_states = array('QLD', 'NSW', 'VIC', 'SA', 'WA', 'NT', 'ACT', 'TAS');

        $state = strtoupper( $_GET['state'] );

        $name = 'dynamicDataState';

        if(in_array($state, $valid_states) ) { 
            setcookie( $name, $state, time() + 3600, "/", COOKIE_DOMAIN );


I've got bit further with this and worked out that I can in fact use an htacess rewrite rule to change the cookie. ...I think it's working with LSCache and the conditional logic... I'm sure it could be written more cleanly though and I still need to get a response header cookie sent back...

RewriteEngine on
RewriteCond %{QUERY_STRING} state=qld
RewriteRule ^(.*)$ - [CO=dynamicDataState:QLD:/:3600]
RewriteCond %{QUERY_STRING} state=new
RewriteRule ^(.*)$ - [CO=dynamicDataState:NSW:/:3600]
RewriteCond %{QUERY_STRING} state=vic
RewriteRule ^(.*)$ - [CO=dynamicDataState:VIC:/:3600]
RewriteCond %{QUERY_STRING} state=wa
RewriteRule ^(.*)$ - [CO=dynamicDataState:WA:/:3600]
RewriteCond %{QUERY_STRING} state=sa
RewriteRule ^(.*)$ - [CO=dynamicDataState:SA:/:3600]
RewriteCond %{QUERY_STRING} state=tas
RewriteRule ^(.*)$ - [CO=dynamicDataState:TAS:/:3600]
RewriteCond %{QUERY_STRING} state=nt
RewriteRule ^(.*)$ - [CO=dynamicDataState:NT:/:3600]
RewriteCond %{QUERY_STRING} state=act
RewriteRule ^(.*)$ - [CO=dynamicDataState:ACT:/:3600]

To add some more info:

  • Users are initially using a drop down select to choose a 'state', this triggers a script that adds the cookie initially and then reloads the page so they see their state specific information on that page and the rest of the site.
  • I have a cache vary setup for this cookie in LSCache so the correct pages are served to users with the cookie set.
  • Once the page is cached, then WP is completely bypassed so any logic I put in there to change the cookie based on URL doesnt work, nor does sending a new cookie back in the response header
  • The above starting point for my htacess solution seems to work so far, but does not send a new cookie header response back so the user still has the old cookie in their browser. I need to resolve this?

So new questions:

Is there a cleaner, more concise way to write the above rewrite?

Is there a way to also conditionally send a response header back even when a cached page is served?

...OR is there a bette way in general to achieve this?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.