1

good day ,

how to create a button on post so that visitors can purge it own post cache on cloudflare ?

i found this bash script and use to manually flush single url

#!/bin/bash
if [ -z "$1" ]; then
    echo "Usage: $0 http://your.domain.com/url"
    exit 0
fi
#get this from Account section in Cloudflare
TOKEN="my_token_"
EMAIL="my_email"
DOMAIN="my_domain"

curl https://www.cloudflare.com/api_json.html \
  -d "a=zone_file_purge" \
  -d "tkn=$TOKEN" \
  -d "email=$EMAIL" \
  -d "z=$DOMAIN" \
  -d "url=$1"

and also this in my functions.php that will purge when post is published/edit

function cloudflare_purge_file( $post_ID )
{
$fields = array(
'a' => 'zone_file_purge',
'tkn' => 'my_token',
'email' => 'my_email',
'z' => 'my_domain',
'url' => get_permalink()
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, 'https://www.cloudflare.com/api_json.html');
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

return $post_ID;
}
add_action('save_post', 'cloudflare_purge_file');
add_action('deleted_post', 'cloudflare_purge_file');
add_action('trashed_post', 'cloudflare_purge_file');
add_action('edit_post', 'cloudflare_purge_file');

is that possible to implement to php code to put on post so user can manually purge the post cache ?

i put this on my post so user can reload the post

<li class="pull-right"><a href="JavaScript: location.reload(true);" title="Reload Pages"><span class="icon_refresh" aria-hidden="true"></span>  <span class="text">&nbsp;Reload Pages</span></a></li>

and after i use cloudflare pages rules with cache everything that code doesnt work anymore .

thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.