Skip to content

Instantly share code, notes, and snippets.

@eksiscloud
Last active June 23, 2025 13:45
Show Gist options
  • Save eksiscloud/3e3fd041c092365a78ece344d46cee9d to your computer and use it in GitHub Desktop.
Save eksiscloud/3e3fd041c092365a78ece344d46cee9d to your computer and use it in GitHub Desktop.
Sends a request to Varnish for bans after a WordPress post is edited and re-published
// Use snippets plugin or functions.php
// Adds xkey tags: frontpage, sidebar (right), article-ID, category and tag
// Remember to change url
add_action('save_post', function ($post_id) {
// Don't do anything is this isn't an article or is autosaving
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (get_post_type($post_id) !== 'post') return;
$tags = [];
$tags[] = 'article-' . $post_id;
$tags[] = 'sidebar';
$tags[] = 'frontpage';
$host = parse_url(home_url(), PHP_URL_HOST);
if (strpos($host, 'poochierevival') !== false) {
$tags[] = 'domain-poochie';
} elseif (strpos($host, 'katiska') !== false) {
$tags[] = 'domain-katiska';
}
// Categories
$categories = get_the_category($post_id);
if (!empty($categories)) {
foreach ($categories as $cat) {
$tags[] = 'category-' . sanitize_title($cat->slug);
}
}
// Tags
$post_tags = get_the_tags($post_id);
if (!empty($post_tags)) {
foreach ($post_tags as $tag) {
$tags[] = 'tag-' . sanitize_title($tag->slug);
}
}
$tags = array_unique($tags);
foreach ($tags as $tag) {
wp_remote_request(home_url('/'), [
'method' => 'BAN',
'headers' => [
'xkey-purge' => $tag
],
'blocking' => false // not waiting for an answer
]);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment