Skip to content

Instantly share code, notes, and snippets.

@emasantos
Last active February 17, 2019 14:21
Show Gist options
  • Save emasantos/e640eaeee58b859040a75fb7a5cb25e7 to your computer and use it in GitHub Desktop.
Save emasantos/e640eaeee58b859040a75fb7a5cb25e7 to your computer and use it in GitHub Desktop.
How to bypass Cloudflare cache with Wordpress
<?php
/*
- Admin users will get a different URL with a bypass argument.
- The original clean URL will be cached in CloudFlare without the admin bar.
- The bypass URL argument ensures that CloudFlare shows you the most recent version of the page/data.
*/
function efAddByPassParamToUrl($url)
{
$time = time();
if (strpos($url, '?') === FALSE){
return $url . '?bypass=' . $time;
}
return $url . '&bypass=' . $time;
}
if (is_admin()) {
add_action('post_link', 'efAddByPassParamToUrl');
add_action('page_link', 'efAddByPassParamToUrl');
add_action('year_link', 'efAddByPassParamToUrl');
add_action('month_link', 'efAddByPassParamToUrl');
add_action('day_link', 'efAddByPassParamToUrl');
add_action('term_link', 'efAddByPassParamToUrl');
add_action('search_link', 'efAddByPassParamToUrl');
add_action('post_type_archive_link', 'efAddByPassParamToUrl');
add_action('preview_post_link', 'efAddByPassParamToUrl');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment