-
Notifications
You must be signed in to change notification settings - Fork 47
Custom Filters
Varnish HTTP Purge has the ability for users to add in filters.
-
vhp_home_url
- Change the home URL (default ishome_url()
) -
vhp_purge_urls
- Add additional URLs to what will be purged -
varnish_http_purge_headers
- Allows you to change the HTTP headers to send with a PURGE request. -
varnish_http_purge_schema
- Allows you to change the schema (default is http) -
varnish_http_purge_url
- Allow you to filter the URL (i.e. changedev.mydomain.com
tomydomain.com
) -
varnish_http_purge_events
- Add a specific event to trigger a page purge -
varnish_http_purge_events_full
- Add a specific event to trigger a full site purge
Let's say you have a custom page that lists all your newest content at example.com/newest
but it's not listed as your 'blog' page in WordPress. When you update a post, this page won't be flushed.
To fix that, you can use the vhp_purge_urls
filter.
<?php
function my_varnish_urls( $urls, $postid ) {
$myurl = 'http://example.com/newest/';
array_push( $urls, $myurl ) ;
return $urls;
}
add_filter( 'vhp_purge_urls', 'my_varnish_urls' );
This will add the URL on to the list of URLs to be purged. If you have multiple URLs, it would look like this:
<?php
function my_varnish_urls( $urls, $postid ) {
$myurls = array (
'http://example.com/newest/',
'http://example.com/oldest/',
'http://example.com/anotherURL/',
);
if ( !empty($myurls) ) {
foreach ($myurls as $url) {
array_push($urls, $url ) ;
}
}
return $urls;
}
add_filter( 'vhp_purge_urls', 'my_varnish_urls' );
Or you could do it this way:
<?php
function my_varnish_urls( $urls, $postid ) {
array_push($urls, 'http://example.com/newest/');
array_push($urls, 'http://example.com/oldest/');
array_push($urls, 'http://example.com/anotherURL');
return $urls;
}
add_filter( 'vhp_purge_urls', 'my_varnish_urls' );
And since you're passing in the Post ID, you can go a step further and check if $postid
is a post with the custom type of Foobar, and if so, purge something else entirely.
By default, Varnish doesn't care about HTTPS. Since most of us are actually using https now, if you need to force https for purging you can do this:
<?php
function my_varnish_schema() {
return 'https://';
}
add_filter( 'varnish_http_purge_schema', 'my_varnish_schema' );
Note: If you're on DreamPress, upgrade to version 5.0 of the plugin and it will automagically switch you to https. This will remain filterable, of course.
In general most people don't need this. But there are rare cases where other plugins update posts and don't trigger the post update commands.
<?php
function my_varnish_purge_events( $actions ) {
$myactions = array(
'updated_postmeta', // When post meta is updated
'some_plugin_event,' // Some plugin event that edits a post w/out triggering update post
);
array_merge( $actions, $myactions ) ;
return $actions;
}
add_filter( 'varnish_http_purge_events', 'my_varnish_purge_events' );
Remember: if you're using varnish_http_purge_events
then your event must return a post ID.
If you don't have a post ID and you still need this, add it to both varnish_http_purge_events_full
and varnish_http_purge_events
- but please use this with caution, otherwise you'll be purging everything all the time, and you're a terrible person.
USE THIS SPARINGLY!!!! This can be computationally expensive if you add it in for something like init
then you will flush your entire cache on every page load. Which ... why? But anyway, here are some examples:
<?php
function my_varnish_purge_events( $actions ) {
$myactions = array(
'profile_update', // After a user profile is updated
'upgrader_process_complete,' // when the download process for a plugin install or update finishes
);
array_merge( $actions, $myactions ) ;
return $actions;
}
add_filter( 'varnish_http_purge_events', 'my_varnish_purge_events' );
function my_varnish_purge_events_full( $actions ) {
$myactions = array(
'profile_update', // After a user profile is updated
'upgrader_process_complete,' // when the download process for a plugin install or update finishes
);
array_merge $actions, $myactions ) ;
return $actions;
}
add_filter( 'varnish_http_purge_events_full', 'my_varnish_purge_events_full' );
To use this properly, you'll need both calls.