Created
July 30, 2025 13:47
-
-
Save ChrisButterworth/314c7cba38d6d113c52c29dc9d84dac4 to your computer and use it in GitHub Desktop.
Purge Varnish on save action
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
<?php | |
// Written but not tested | |
add_action('save_post', function ($post_id, $post, $update) { | |
// Find a reason not to fire | |
// Serving rest request | |
if (!wp_is_serving_rest_request()) { | |
return; | |
} | |
// Doing an autosave | |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { | |
return; | |
} | |
$is_autosave = wp_is_post_autosave($post_id); | |
if ($is_autosave) { | |
return; | |
} | |
// If we're here, we're all good | |
if($update) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, get_permalink($post_id); | |
// Setting request to PURGE | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PURGE"); | |
curl_exec($ch); | |
curl_close($ch); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can add archive URL's if not a page (if used) or a custom post type.