Skip to content

Instantly share code, notes, and snippets.

@ChrisButterworth
Created July 30, 2025 13:47
Show Gist options
  • Save ChrisButterworth/314c7cba38d6d113c52c29dc9d84dac4 to your computer and use it in GitHub Desktop.
Save ChrisButterworth/314c7cba38d6d113c52c29dc9d84dac4 to your computer and use it in GitHub Desktop.
Purge Varnish on save action
<?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);
}
});
@ChrisButterworth
Copy link
Author

Can add archive URL's if not a page (if used) or a custom post type.

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