Skip to content

Instantly share code, notes, and snippets.

@williameliel
Created June 1, 2015 16:45
Show Gist options
  • Save williameliel/cb612466711203280332 to your computer and use it in GitHub Desktop.
Save williameliel/cb612466711203280332 to your computer and use it in GitHub Desktop.
Purge Varnish on Save
function varnish_purge($post_id)
{
$hostname = 'localhost';
$port = 80;
$URL = get_permalink( $post_id );
$debug = false;
// $finalURL = sprintf(
// "http://%s:%d%s", $hostname, $port, $URL
// );
$finalURL = $URL;
$curlOptionList = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'PURGE',
CURLOPT_HEADER => true ,
CURLOPT_NOBODY => true,
CURLOPT_URL => $finalURL,
CURLOPT_CONNECTTIMEOUT_MS => 2000
);
$fd = false;
if( $debug == true ) {
print "-- Curl debug -----\n";
$fd = fopen("php://output", 'w+');
$curlOptionList[CURLOPT_VERBOSE] = true;
$curlOptionList[CURLOPT_STDERR] = $fd;
}
$curlHandler = curl_init();
curl_setopt_array( $curlHandler, $curlOptionList );
curl_exec( $curlHandler );
curl_close( $curlHandler );
if( $fd !== false ) {
fclose( $fd );
}
}
add_action( 'save_post', 'varnish_purge' );
@williameliel
Copy link
Author

Local IP or internal address for server must in the purgers IPs ie:

acl purge {
"localhost";
"127.0.0.1";
"10.210.66.197";
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment