ttsvetko
5/17/2010 - 8:29 PM

Simple WordPress plugin for repointing resource URLs to a CDN pull style host

Simple WordPress plugin for repointing resource URLs to a CDN pull style host

<?php
/*
Plugin Name: Sivel CDN
Plugin URI:   http://sivel.net/
Description:  Points media urls at a CDN
Version:      1.0
Author:       Matt Martz
Author URI:   http://sivel.net/
*/

$origin = 'http://sivel.net';
$cdn = 'http://cache.sivel.net';

if ( ! is_admin() ) :

add_filter('script_loader_src', 'change_source');
add_filter('style_loader_src', 'change_source');
add_filter('stylesheet_uri', 'change_source');
add_filter('stylesheet_directory_uri', 'change_source');
function change_source($src) {
    global $origin, $cdn;
    if ( ! stristr($src, '.php') )
        $src = str_replace($origin, $cdn, $src);
    return $src;
}

endif;

add_filter('pre_option_upload_url_path', 'cdn_upload_url');
function cdn_upload_url($url) {
    global $origin, $cdn;
    return str_replace($origin, $cdn, $url);
}