kodansha / wack-cloudfront-invalidation
Invalidate CloudFront caches when a post is published or updated.
Installs: 238
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:wordpress-plugin
pkg:composer/kodansha/wack-cloudfront-invalidation
Requires
- php: >=8.1
- aws/aws-sdk-php: ^3.342
Requires (Dev)
- 10up/wp_mock: ^1.0
README
WACK CloudFront Invalidation is a WordPress plugin that helps you to run invalidation on CloudFront after a post is published or updated.
It is designed to work with the WACK Stack, but can be used with any WordPress installation that uses CloudFront as a CDN.
How it works
This plugin triggers invalidation for published content updates by using:
save_postrest_after_insert_{post_type}(forpostand public custom post types)
It skips autosaves, revisions, and unpublished states such as draft and
auto-draft.
Installation
- Requires PHP 8.1 or later
- Requires WordPress 6.7 or later
- Requires Composer
Using Composer
composer require kodansha/wack-cloudfront-invalidation
Note
This plugin is not available on the WordPress.org plugin repository. The only installation method currently available is through Composer.
Configuration
AWS credentials
WACK CloudFront Invalidation uses AWS SDK for PHP to send invalidation requests to CloudFront, so you need to configure AWS credentials. Please refer to the AWS SDK for PHP documentation for more information on how to set up credentials.
CloudFront Distribution ID
You can configure a CloudFront Distribution ID in either of the following ways:
- Set
WACK_CF_INV_DISTRIBUTION_IDinwp-config.php(or equivalent) - Enter it in the plugin settings page (
WACK Stack>WACK CF Inv)
define('WACK_CF_INV_DISTRIBUTION_ID', 'YOUR_CLOUDFRONT_DISTRIBUTION_ID');
Important
If WACK_CF_INV_DISTRIBUTION_ID is defined, it takes precedence over the value stored in the database.
Note
This plugin supports only one CloudFront Distribution ID.
Invalidation Path Settings
After installing the WACK CloudFront Invalidation plugin, a settings page is added to the WordPress admin menu under WACK Stack > WACK CF Inv.
In this settings page, you can configure CloudFront invalidation paths for each post type.
Important
To specify multiple paths, list them separated by line breaks. Empty lines will be ignored.
You can use the following placeholders in each path:
%id%: Replaced with the post ID%slug%: Replaced with the post slug (falls back to post ID when slug is empty)
wack_cf_inv_{post_type}_paths filter
This plugin allows you to customize the CloudFront invalidation paths per post type using a filter.
The following filter is applied before issuing a CloudFront invalidation:
$paths = apply_filters('wack_cf_inv_' . $post->post_type . '_paths', $paths, $post);
{post_type}: Replace with your custom post type (e.g.,post,news, etc.).$paths: Array of paths determined by the post type and the plugin settings.$post: TheWP_Postobject being processed.
Example
To add the homepage (/) to the invalidation paths for the post post type,
add the following to your theme’s functions.php or a custom plugin:
add_filter('wack_cf_inv_post_paths', function($paths, $post) { $paths[] = '/'; return $paths; }, 10, 2);
This filter allows you to modify or extend the list of paths to be invalidated for each post type, providing flexible cache control.
Tip
If you want to modify or extend the list of paths for the news post type,
use wack_cf_inv_news_paths instead of wack_cf_inv_post_paths.
Tips
WACK_CF_INV_DRY_RUN constant
Dry Run can be configured either by constant (WACK_CF_INV_DRY_RUN) or from
the plugin settings page.
When WACK_CF_INV_DRY_RUN is defined, its value takes precedence over the
database setting.
If Dry Run is enabled, the actual CloudFront invalidation execution will be skipped, and only log output will be performed.
You can use this for testing behavior in development environments.
WACK Log integration
If you have the WACK Log plugin installed, various logs from WACK CloudFront Invalidation will be output to standard output using WACK Log.