My CDN code
There are lots of “recommended” ways to do CDN’s in WordPress. Personally, I prefer to keep it simple and use the following code as an mu-plugin to simply buffer the complete output and rewrite the URL’s to match whatever you require. The URL’s to be rewritten can be controlled by simply filtering the ‘ryans_cdn_rewrites’ filter as shown further down the page.
<?php /** * Ryan's simple URL rewriter for CDN's. * * @copyright Copyright (c), Ryan Hellyer * @license http://www.gnu.org/licenses/gpl.html GPL * @author Ryan Hellyer <ryanhellyer@gmail.com> * @since 1.0 */ class Ryans_CDN { /** * Class constructor. */ public function __construct() { // Allow for ignoring CDN during testing if ( defined( 'IGNORE_RYANS_CDN' ) ) { return; } add_action( 'template_redirect', array( $this, 'template_redirect' ) ); add_filter( 'rewrite_urls', array( $this, 'filter' ) ); } /* * Filtering URLs. * * @param string $content The content to be filtered * @return string $content The modified content after URL rewriting * */ public function filter( $content ) { // This is a critical point at which you must add rules for rewriting URL's $rewrites = apply_filters( 'ryans_cdn_rewrites', array() ); // Loop through each rule and process it foreach( $rewrites as $origin => $destination ) { $content = str_replace( $origin, $destination, $content ); } return $content; } /* * Starting page buffer. */ public function template_redirect() { ob_start( array( $this, 'ob' ) ); } /* * Rewriting URLs once buffer ends. * * @return string The filtered page output including rewritten URLs. */ public function ob( $contents ) { return apply_filters( 'rewrite_urls', $contents, $this ); } } new Ryans_CDN;
And here is the filter to control what URL’s are rewritten. You should modify this to suit your requirements as most sites will have specific requirements for how their URL rewrites are handled.
<?php /** * Demo function which rewrites URLs to add a subdomain of .cdn. * * @param [type] $rewrites [description] * @return [type] [description] */ function ryans_cdn_rewrites( $rewrites ) { $urls = array( home_url( '/wp-content/uploads/' ), home_url( '/wp-content/themes/' ), home_url( '/wp-content/plugins/' ), ); foreach( $urls as $in => $out ) { $rewrites[$out] = str_replace( '://', '://cdn.', $urls[$in] ); } return $rewrites; } add_filter( 'ryans_cdn_rewrites', 'ryans_cdn_rewrites' );
Here is another demo function can be used to dynamically convert http to https for a specific domain. Note, this function has not been tested yet.
<?php /** * Demo rewrite function. * Rewrites http to https for a specific domain. * * @param [type] $rewrites [description] * @return [type] [description] */ function ryans_https_rewrites( $rewrites ) { $rewrites['http://domain.com'] = 'https://domain.com'; return $rewrites; } add_filter( 'ryans_cdn_rewrites', 'ryans_https_rewrites' );
John Parris says:
Hey Ryan, handy little plugin there. I\’m curious how you manage getting the media assets to the CDN for this to work?
January 18, 2015 at 5:15 pm # //
Ryan says:
I usually use a pull CDN like Amazon Cloudfront and load them directly from their usual location in the WordPress uploads folder. Making the new URL work is then just a server config. issue.
January 18, 2015 at 8:39 pm # //
Brian Jackson says:
Hey Ryan,
I can tell you love things that are lightweight. I personally try not to install plugins whenever I can get around it as well. But have you seen this new plugin called CDN Enabler? Super lightweight and pretty much easiest way to integrate CDN in minutes.
https://wordpress.org/plugins/cdn-enabler/
September 9, 2015 at 7:03 pm # //
Ryan says:
Thanks. That looks like a very good plugin, and it works in a similar way to my own plugin. I’ve favourited it on WordPress.org in case I need it in future 🙂
January 20, 2016 at 2:23 pm # //