Skip to content

Instantly share code, notes, and snippets.

@Anan5a
Created October 14, 2018 09:56
Show Gist options
  • Save Anan5a/0181bb63ef70b79085ce324ded8cead0 to your computer and use it in GitHub Desktop.
Save Anan5a/0181bb63ef70b79085ce324ded8cead0 to your computer and use it in GitHub Desktop.
<?php
add_action('save_post', 'purge_cache');
function purge_cache($post_ID)
{
$url = get_permalink($post_ID, true);
echo ping_cloudflare($url);}
function ping_cloudflare($url)
{
$body = array();
$body['files'] = [$url];
$api_key = 'key';
$email = '[email protected]';
$zone_id = 'id';
$endpoint = "https://api.cloudflare.com/client/v4/zones/$zone_id/purge_cache";
$http_settings = array(
'method'=>'POST',
'headers'=>array(
'Content-Type'=>'application/json',
'X-Auth-Email'=>$email,
'X-Auth-Key'=>$api_key,
),
'body'=>json_encode($body),
'data_format'=>'body'
);
$response = wp_remote_request($endpoint, $http_settings);
$response_body = wp_remote_retrieve_body($response);
$result = json_decode($response_body);
if ($result->success == true) {
echo "Cache PURGED";
}else{
echo "Purge FAILED";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment