Serve different cache based on GET parameter
-
I have content that is only available trough a newsletter, the URL in the newsletter adds a query parameter. lets say https://foobar.com/my-post/?my_query_param=12345.
If i come on the page WITH my query parameter i should be able to read the post and its content without a paywall. If i come on the page without the query parameter i should get a paywall. This works without caching.
Now i have added a dynamic caching plugin altering the cache key with the cacheaction wp_cache_key. The code looks like this:
//Get query parameter if it is present in the URL $inputString = $_SERVER['QUERY_STRING']; $parameters = []; parse_str($inputString, $parameters); //This is either INT, 0 or false. $my_query_param = isset($parameters['my_query_param']) ? intval($parameters['my_query_param']) : false; if( !is_user_logged_in() ) { if( $my_query_param === false ) { //No query parameter, paywall return $slug .= '_not_logged_in'; } else if( $my_query_param === 0 ){ //Invalid parameter, paywall return $slug .= '_not_logged_in_newsletter'; } else{ //Content without paywall return $slug .= '_not_logged_in_free_content'; } }so $my_query_param can be a number, 0 or false. I get a different cache key in either situation. However a post without the query parameter gets served the same cache when i visit the same post WITH a query parameter.
I believe this is a bug but if i am missing something i would appreciate the solution.
The topic ‘Serve different cache based on GET parameter’ is closed to new replies.