WordPress CDN Enabler Plugin
The WordPress CDN Enabler plugin is a content delivery network (CDN) integration plugin that rewrites URLs, such as those serving CSS, JavaScript, and images, to be served by a CDN. It supports including URLs in the rewrite by file extension and excluding URLs in the rewrite by string. When a KeyCDN account is connected the CDN cache can be purged from WordPress. This simple, lightweight plugin makes integrating a WordPress website with a CDN a breeze.
Installation
Installation of the WordPress CDN Enabler plugin is easy and can be done in just a few steps:
- Log in to your WordPress admin dashboard.
- In the left navigation sidebar hover over Plugins and then click Add New.
- Either upload the latest CDN Enabler version in .zip format, which can be directly downloaded here, or search for "CDN Enabler" and click Install Now on the plugin by KeyCDN.
- After the CDN Enabler plugin has been installed activate the plugin.
CDN Enabler will now be successfully installed and activated on your WordPress website. We have built this plugin to work for the majority of WordPress installations right out of the box, which means all that is required is to provide a CDN Hostname for it to start rewriting your URLs. However, you may want to further configure CDN Enabler to fit your needs, which can be done through the plugin settings and/or hooks.
Settings
Customizing the CDN Enabler plugin settings can be done by going to the CDN Enabler settings page. This page can be accessed by doing the following:
- Log in to your WordPress admin dashboard.
- In the left navigation sidebar hover over Settings and then click CDN Enabler.
The CDN Enabler settings page is simple and broken into three main parts, which are the CDN Hostname, CDN Inclusions, and CDN Exclusions. These settings define what will be included in the rewrite, what will not, and the new hostname that the rewritten URLs will contain. The last part allows a KeyCDN account to be connected to be able to purge the CDN cache directly from WordPress. Optionally validate the configuration to ensure URLs that will be rewritten will come from a valid hostname.
Hooks
Name | Type | Description |
---|---|---|
cdn_enabler_user_can_purge_cache | Filter | Filters whether the current user can purge the cache. |
cdn_enabler_exclude_admin | Filter | Filters whether admin pages should bypass the rewrite. |
cdn_enabler_bypass_rewrite | Filter | Filters whether the rewrite should be bypassed. |
cdn_enabler_site_hostnames | Filter | Filters the site hostnames that should be rewritten. |
cdn_enabler_rewrite_relative_urls | Filter | Filters whether relative URLs should be rewritten. |
cdn_enabler_contents_before_rewrite | Filter | Filters contents before being rewritten. |
cdn_enabler_contents_after_rewrite | Filter | Filters contents after being rewritten. |
cdn_enabler_user_can_purge_cache
apply_filters( 'cdn_enabler_user_can_purge_cache', bool $user_can_purge_cache = current_user_can( 'manage_options' ) );
Filters whether the current user can purge the cache.
Parameters
$user_can_purge_cache
(bool) Whether the current user can purge the cache. Defaults to value returned by current_user_can( 'manage_options' )
.
Source
File: inc/cdn_enabler.class.php
Changelog
2.0.0 - Introduced.
Example
add_filter( 'cdn_enabler_user_can_purge_cache', 'current_user_can_edit_posts' );
function current_user_can_edit_posts() {
return current_user_can( 'edit_posts' );
}
cdn_enabler_exclude_admin
apply_filters( 'cdn_enabler_exclude_admin', bool $exclude_admin = is_admin() );
Filters whether admin pages should bypass the rewrite.
Parameters
$exclude_admin
(bool) Whether admin pages should bypass the rewrite. Defaults to value returned by is_admin()
.
Source
File: inc/cdn_enabler_engine.class.php
Changelog
2.0.1 - Introduced.
Example
add_filter( 'cdn_enabler_exclude_admin', '__return_false' );
cdn_enabler_bypass_rewrite
apply_filters( 'cdn_enabler_bypass_rewrite', bool $bypass_rewrite = false );
Filters whether the rewrite should be bypassed.
Parameters
$bypass_rewrite
(bool) Whether the rewrite should be bypassed. Default is false
.
Source
File: inc/cdn_enabler_engine.class.php
Changelog
2.0.0 - Introduced.
Example
add_filter( 'cdn_enabler_bypass_rewrite', '__return_true' );
cdn_enabler_site_hostnames
apply_filters( 'cdn_enabler_site_hostnames', array $site_hostnames = array( $site_hostname ) );
Filters the site hostnames that should be rewritten.
Parameters
$site_hostnames
(array) Site hostnames that should be rewritten. Default $site_hostname
comes from the Host
request header value, or if that is empty, the Site Address (URL) setting returned by home_url()
.
Source
File: inc/cdn_enabler_engine.class.php
Changelog
2.0.0 - Introduced.
Example
add_filter( 'cdn_enabler_site_hostnames', 'filter_cdn_enabler_site_hostnames' );
function filter_cdn_enabler_site_hostnames( $site_hostnames ) {
$site_hostnames[] = 'www.example.com'; // also rewrite URLs containing "www.example.com"
return $site_hostnames;
}
cdn_enabler_rewrite_relative_urls
apply_filters( 'cdn_enabler_rewrite_relative_urls', bool $rewrite_relative_urls = true );
Filters whether relative URLs should be rewritten.
Parameters
$rewrite_relative_urls
(bool) Whether relative URLs should be rewritten. Default is true
.
Source
File: inc/cdn_enabler_engine.class.php
Changelog
2.0.0 - Introduced.
Example
add_filter( 'cdn_enabler_rewrite_relative_urls', '__return_false' );
cdn_enabler_contents_before_rewrite
apply_filters( 'cdn_enabler_contents_before_rewrite', string $contents );
Filters contents before being rewritten.
Parameters
$contents
(string) Contents, such as JSON or raw HTML from the output buffer, before going through the rewriter.
Source
File: inc/cdn_enabler_engine.class.php
Changelog
2.0.1 - Introduced.
Example
add_filter( 'cdn_enabler_contents_before_rewrite', 'filter_cdn_enabler_contents_before_rewrite' );
function filter_cdn_enabler_contents_before_rewrite( $contents ) {
// code...
}
cdn_enabler_contents_after_rewrite
apply_filters( 'cdn_enabler_contents_after_rewrite', string $rewritten_contents );
Filters contents after being rewritten.
Parameters
$rewritten_contents
(string) Rewritten contents, such as JSON or raw HTML from the output buffer, after going through the rewriter.
Source
File: inc/cdn_enabler_engine.class.php
Changelog
2.0.1 - Introduced.
Example
add_filter( 'cdn_enabler_contents_after_rewrite', 'filter_cdn_enabler_contents_after_rewrite' );
function filter_cdn_enabler_contents_after_rewrite( $rewritten_contents ) {
$rewritten_contents = str_replace( 'cdn.example.com/', 'cdn.example.com/path/', $rewritten_contents );
$rewritten_contents = str_replace( 'cdn.example.com\/', 'cdn.example.com\/path\/', $rewritten_contents );
return $rewritten_contents;
}
WP-CLI
Interact with CDN Enabler from the command line by purging the CDN cache. This can be done through WP-CLI with the cdn-enabler
command.
cdn-enabler
Purge the CDN cache with the cdn-enabler
command and purge
subcommand.
Example
# Purge the CDN cache.
$ wp cdn-enabler purge
FAQ
Find answers to commonly asked questions about CDN Enabler. Still have questions? Open a new topic in the WordPress Support Forums and we would be happy to help.
How can I check if CDN Enabler is working on my site?
You can quickly verify that the CDN Enabler is rewriting your URLs to be served by the designated CDN by checking the page source. Instead of your origin URL delivering your assets (e.g. https://www.example.com/wp-content/uploads/examle.jpg
) it will be your CDN (e.g. https://cdn.example.com/wp-content/uploads/example.jpg
).