Created
June 1, 2015 16:45
-
-
Save williameliel/cb612466711203280332 to your computer and use it in GitHub Desktop.
Purge Varnish on Save
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Local IP or internal address for server must in the purgers IPs ie:
acl purge {
"localhost";
"127.0.0.1";
"10.210.66.197";
}