Change template_directory to use a CDN path

Posted on 16th September, 2015 Leave a Comment

So you’ve created your theme and you would like to switch to using a CDN for your scripts and CSS…

Add this to your functions.php and replace the YOUR-SITE with the urls of the website and the CDN.

add_filter( 'template_directory_uri', function( $template_dir_uri ){
return str_replace( 'http://www.YOUR-SITE.co.uk/', '//cdn.YOUR-SITE.co.uk/', $template_dir_uri );
});

Leave a Comment

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]