Last active
January 28, 2016 22:23
-
-
Save soderlind/90226375f0e93ae2c635 to your computer and use it in GitHub Desktop.
remove http and https from links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//code from https://wordpress.org/plugins/cloudflare/ | |
/** | |
* remove http and https from links, save in mu-plugins | |
*/ | |
if (true !== function_exists('cloudflare_buffer_wrapup')) { | |
function cloudflare_buffer_wrapup($buffer) { | |
// Check for a Content-Type header. Currently only apply rewriting to "text/html" or undefined | |
$headers = headers_list(); | |
$content_type = null; | |
foreach ($headers as $header) { | |
if (strpos(strtolower($header), 'content-type:') === 0) { | |
$pieces = explode(':', strtolower($header)); | |
$content_type = trim($pieces[1]); | |
break; | |
} | |
} | |
if (is_null($content_type) || substr($content_type, 0, 9) === 'text/html') { | |
// replace href or src attributes within script, link, base, and img tags with just "//" for protocol | |
$re = "/(<(script|link|base|img|form)([^>]*)(href|src|action)=[\"'])https?:\\/\\//i"; | |
$subst = "$1//"; | |
$return = preg_replace($re, $subst, $buffer); | |
// on regex error, skip overwriting buffer | |
if ($return) { | |
$buffer = $return; | |
} | |
} | |
return $buffer; | |
} | |
function cloudflare_buffer_init() { | |
// load just the single option, defaulting to on | |
//$cloudflare_protocol_rewrite = load_protocol_rewrite(); | |
//if ($cloudflare_protocol_rewrite == 1) { | |
ob_start('cloudflare_buffer_wrapup'); | |
//} | |
} | |
add_action('plugins_loaded', 'cloudflare_buffer_init'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment