Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

WordPress CloudFlare Url Replacement

View wp-cloudflare-url-replacement.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
<?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
Something went wrong with that request. Please try again.