โค้ดล้างแคช purge cache Cloudflare without Plugin WordPress

purge cache Cloudflare without Plugin WordPress
purge cache Cloudflare without Plugin WordPress

วิธีล้างแคช cloudflare ง่ายๆ กดใช้งานได้จากตรง admin menubar ด้านของ WordPress เพียงแค่เพิ่มโค้ดในไฟล์ function.php ของ theme ที่ใช้งานอยู่

อย่าลืมใส่ อีเมล์ + API(อยู่ในหน้า profile) + domain ที่ใช้งานกับ cloudflare เพื่อใช้งานง่าย ๆ ได้เลย

โดยไม่ต้องลง plugin พวก purge cache Cloudflare ใด ๆ ทั้งสิ้น


edit /wp-content/themes/namexyz/functions.php
add_action('init', 'taxze_purge_cloudflare');
function taxze_purge_cloudflare(){
if ( isset($_GET['taxzePurgeCloudflare']) ){
//https://stackoverflow.com/questions/56075107/purging-cloudflare-cache-through-their-api-in-php
//Credentials for Cloudflare
$cust_email = ''; //[email protected]
$cust_xauth = ''; //global api, retrieved from the backend after loggin in
$cust_domain = ''; //domain.tld, the domain you want to control

if($cust_email == "" || $cust_xauth == "" || $cust_domain == "") return;

//Get the Zone-ID from Cloudflare since they don't provide that in the Backend
$ch_query = curl_init();
curl_setopt($ch_query, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones?name=".$cust_domain."&status=active&page=1&per_page=5&order=status&direction=desc&match=all");
curl_setopt($ch_query, CURLOPT_RETURNTRANSFER, 1);
$qheaders = array(
'X-Auth-Email: '.$cust_email.'',
'X-Auth-Key: '.$cust_xauth.'',
'Content-Type: application/json'
);
curl_setopt($ch_query, CURLOPT_HTTPHEADER, $qheaders);
$qresult = json_decode(curl_exec($ch_query),true);
curl_close($ch_query);

$cust_zone = $qresult['result'][0]['id'];

//Purge the entire cache via API
$ch_purge = curl_init();
curl_setopt($ch_purge, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/".$cust_zone."/purge_cache");
curl_setopt($ch_purge, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch_purge, CURLOPT_RETURNTRANSFER, 1);
$headers = [
'X-Auth-Email: '.$cust_email,
'X-Auth-Key: '.$cust_xauth,
'Content-Type: application/json'
];
$data = json_encode(array("purge_everything" => true));
curl_setopt($ch_purge, CURLOPT_POST, true);
curl_setopt($ch_purge, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch_purge, CURLOPT_HTTPHEADER, $headers);

$result = json_decode(curl_exec($ch_purge),true);
curl_close($ch_purge);

//Tell the user if it worked //txz: edit your localtime strtotime(...);
if($result['success']==1) echo "<center>Cloudflare Cache successfully purged! Changes should be visible right away.<br>If not try clearing your Browser Cache by pressing \"Ctrl+F5\"<br>".date("Y-m-d h:i:sa", strtotime("+7 hours"))."</center>";
else echo "<center>Error purging Cloudflare Cache. Please log into Cloudflare and purge manually!</center>";


/* //txz: delete server diskcache
$files = glob('../wp-content/cache/gov-cache/objects/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file)) {
unlink($file); // delete file
}
}

$files = glob('../wp-content/cache/gov-cache/fragments/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file)) {
unlink($file); // delete file
}
}*/


}
}

add_action('admin_bar_menu', 'add_toolbar_taxze_purge_cloudflare', 99);
function add_toolbar_taxze_purge_cloudflare($admin_bar){
$admin_bar->add_menu( array(
'id' => 'taxze-purge-cloudflare',
'title' => 'purge.Cloudflare',
'href' => '/wp-admin/admin.php?taxzePurgeCloudflare=1&'.rand(),
'meta' => array(
'title' => __('purge cloudflare'),
'target' => '_blank',
),
));
}

with cURL commandline
vim purgecf.sh
#edit your zoneid,email,globalapi and urldata
curl -s -X POST "https://api.cloudflare.com/client/v4/zones/yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy/purge_cache" \
-H "X-Auth-Email: [email protected]" \
-H "X-Auth-Key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
-H "Content-Type: application/json" \
--data '{"files":["https://www.taxze.com"

,"https://www.taxze.com/faq/"
,"https://www.taxze.com/wp-sitemap-posts-post-1.xml"

]}'


with cURL commandline + textfile.txt
echo "https://www.taxze.com" >> urlc.txt

vim purgecftxt.sh
#edit your zoneid,email,globalapi and urldata
cat urlc.txt | xargs -I{} -n1 -P10 sh -c 'printf "\n--- %s \n" {};curl -X POST "https://api.cloudflare.com/client/v4/zones/yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy/purge_cache" -H "X-Auth-Email: [email protected]" -H "X-Auth-Key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -H "Content-Type: application/json" --data "{\"files\":[\"{}\"]}"'

#for preload
cat urlc.txt | xargs -I{} -n1 -P10 sh -c 'printf "\n--- %s \n" {}; curl -Is {} | grep -E "HTTP|-Status|-status"'

Blog | ,
Line it!