Switch branches/tags
Find file
Fetching contributors…
Cannot retrieve contributors at this time
98 lines (89 sloc) 5.6 KB
Plugin Name: CDN Linker
Plugin URI:
Description: Replaces the blog URL by another for all files under <code>wp-content</code> and <code>wp-includes</code>. That way static content can be handled by a CDN by origin pull - the origin being your blog address - or loaded from an other site.
Version: 1.3.1
Author: W-Mark Kubacki
Author URI:
License: RPL for non-commercial
if ( @include_once('cdn-linker-base.php') ) {
add_action('template_redirect', 'do_ossdl_off_ob_start');
/********** WordPress Administrative ********/
function ossdl_off_activate() {
add_option('ossdl_off_cdn_url', get_option('siteurl'));
add_option('ossdl_off_include_dirs', 'wp-content,wp-includes');
add_option('ossdl_off_exclude', '.php');
add_option('ossdl_off_rootrelative', '');
register_activation_hook( __FILE__, 'ossdl_off_activate');
function ossdl_off_deactivate() {
// register_deactivation_hook( __FILE__, 'ossdl_off_deactivate');
// Deactivated because: If the user activated this plugin again his previous settings would have been deleted by function.
/********** WordPress Interface ********/
add_action('admin_menu', 'ossdl_off_menu');
function ossdl_off_menu() {
add_options_page('CDN Linker', 'CDN Linker', 8, __FILE__, 'ossdl_off_options');
function ossdl_off_options() {
if ( isset($_POST['action']) && ( $_POST['action'] == 'update_ossdl_off' )){
update_option('ossdl_off_cdn_url', $_POST['ossdl_off_cdn_url']);
update_option('ossdl_off_include_dirs', $_POST['ossdl_off_include_dirs'] == '' ? 'wp-content,wp-includes' : $_POST['ossdl_off_include_dirs']);
update_option('ossdl_off_exclude', $_POST['ossdl_off_exclude']);
update_option('ossdl_off_rootrelative', !!$_POST['ossdl_off_rootrelative']);
$example_cdn_uri = str_replace('http://', 'http://cdn.', str_replace('www.', '', get_option('siteurl')));
?><div class="wrap">
<h2>CDN Linker</h2>
<p>Many Wordpress plugins misbehave when linking to their JS or CSS files, and yet there is no filter to let your old posts point to a statics' site or CDN for images.
Therefore this plugin replaces at any links into <code>wp-content</code> and <code>wp-includes</code> directories (except for PHP files) the <code>blog_url</code> by the URL you provide below.
That way you can either copy all the static content to a dedicated host or mirror the files at a CDN by <a href="" target="_blank">origin pull</a>.</p>
<p><strong style="color: red">WARNING:</strong> Test some static urls e.g., <code><?php echo(get_option('ossdl_off_cdn_url') == get_option('siteurl') ? $example_cdn_uri : get_option('ossdl_off_cdn_url')); ?>/wp-includes/js/prototype.js</code> to ensure your CDN service is fully working before saving changes.</p>
<p><form method="post" action="">
<table class="form-table"><tbod>
<tr valign="top">
<th scope="row"><label for="ossdl_off_cdn_url">CDN URL</label></th>
<input type="text" name="ossdl_off_cdn_url" value="<?php echo(get_option('ossdl_off_cdn_url')); ?>" size="64" class="regular-text code" />
<span class="description">The new URL to be used in place of <?php echo(get_option('siteurl')); ?> for rewriting. No trailing <code>/</code> please. E.g. <code><?php echo($example_cdn_uri); ?></code>.
You can use <code>%4%</code> (number between 1 and 9, surrounded by percent signs) to enable up to that many hostname variations.
1 or less doesn't make sense and more than 4 is beyond optimum. If you are going to use 3 or more, then make sure they have different IPs or
<a href="">some routers will block requests</a> to them.
<tr valign="top">
<th scope="row"><label for="ossdl_off_rootrelative">rewrite root-relative refs</label></th>
<input type="checkbox" name="ossdl_off_rootrelative" <?php echo(!!get_option('ossdl_off_rootrelative') ? 'checked="1" ' : '') ?>value="true" class="regular-text code" />
<span class="description">Check this if you want to have links like <code><em>/</em>wp-content/xyz.png</code> rewritten - i.e. without your blog's domain as prefix.</span>
<tr valign="top">
<th scope="row"><label for="ossdl_off_include_dirs">include dirs</label></th>
<input type="text" name="ossdl_off_include_dirs" value="<?php echo(get_option('ossdl_off_include_dirs')); ?>" size="64" class="regular-text code" />
<span class="description">Directories to include in static file matching. Use a comma as the delimiter. Default is <code>wp-content, wp-includes</code>, which will be enforced if this field is left empty.</span>
<tr valign="top">
<th scope="row"><label for="ossdl_off_exclude">exclude if substring</label></th>
<input type="text" name="ossdl_off_exclude" value="<?php echo(get_option('ossdl_off_exclude')); ?>" size="64" class="regular-text code" />
<span class="description">Excludes something from being rewritten if one of the above strings is found in the match. Use a comma as the delimiter. E.g. <code>.php, .flv, .do</code>, always include <code>.php</code> (default).</span>
<input type="hidden" name="action" value="update_ossdl_off" />
<p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /></p>