Skip to content

Instantly share code, notes, and snippets.

@miziomon
Created April 21, 2012 13:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miziomon/2437025 to your computer and use it in GitHub Desktop.
Save miziomon/2437025 to your computer and use it in GitHub Desktop.
WordPress CloudFlare Url Replacement
<?php
/*
Plugin Name: CloudFlare URL Replacement
Plugin URI: http://maurizio.mavida.com/wordpress-cloudflare-url-replacement/
Description: Super simle replacer for CloudFlare user.
License: GPL
Version: 0.6
Author: Maurizio Pelizzone
Author URI: http://maurizio.mavida.com
Installation:
** This is a beta version - no guarantee for production use
Place the cloudflare-url-replacement.php file in your /wp-content/plugins/ directory
Edit $CloudFlareUrl variabile according with Cloud Flare admin panel
[before activate] Check your CloudFlare Account and verify that DNS have been updated
Activate through the WordPress administration panel.
==========================================================================
License: GPL
CloudFlare URL Replacement Plugin
Copyright (C) 2012, Maurizio Pelizzone, http://maurizio.mavida.com
All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the
Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor
Boston, MA 02110-1301, USA
*/
Class CloudFlareUrlReplace {
/*
* Attention:
* you need to edit $CloudFlareUrl with your static subdomain according in cloudflare panel (without http)
* es. s1.your-server.com
*/
private $CloudFlareUrl = "static.mavidacdn.in";
private $blog_url = "";
public function __construct( ) {
// check if CloudFlareUrl is set
if ( !is_admin() && ($this->CloudFlareUrl != "") ) {
$this->blog_url = str_replace("http://", "", get_bloginfo("home"));
$this->CloudFlareUrl = str_replace("http://", "", $this->CloudFlareUrl);
add_filter('the_content', array( $this , 'CloudFlareImageReplace') );
add_filter('post_thumbnail_html', array( $this ,'CloudFlareImageReplace') );
add_filter('widget_text', array( $this ,'CloudFlareImageReplace') );
add_filter('script_loader_src', array( $this ,'CloudFlareScriptReplace') );
}
}
/**
* hook to replace finded link in post_content, widgets and image thumbnail call with get_the_post_thumbnail
*/
function CloudFlareImageReplace ( $content ) {
$pattern="/(" .$this->blog_url . ")(\/wp-content\/)(.*)(png|gif|jpg)/";
$replacement = $this->CloudFlareUrl . "$2$3$4";
return preg_replace($pattern, $replacement,$content );
}
/**
* hook to replace link to all enqueue_script / enqueue_style
*/
function CloudFlareScriptReplace ( $src ) {
$pattern="/(" . $this->blog_url . ")(\/wp-content\/)(.*)(js|css)/";
$replacement = $this->CloudFlareUrl . "$2$3$4";
// remove Query Strings From Static Resources
$src_parts = explode('?', $src);
return preg_replace($pattern, $replacement, $src_parts[0] );
}
}
new CloudFlareUrlReplace();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment