Last active
August 29, 2015 13:59
-
-
Save thisiskylierose/331c154f89e7ae0ca56e to your computer and use it in GitHub Desktop.
Wordpress functions to run image replace for timthumb
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
/*-----------------------------------------------------------------------------------*/ | |
/* Replace image src values | |
/*-----------------------------------------------------------------------------------*/ | |
if (!function_exists('ic_replace_image_src')) { | |
function ic_replace_image_src($img = null) { | |
$live = 'mysite.com.au '; | |
if ($_SERVER['HTTP_HOST'] == $live) { | |
return; | |
} else { | |
$replace = ic_set_image_src(); | |
if (preg_match('/mysite.co.nz/', $img)) { | |
$pattern = '/mysite.co.nz/'; | |
$result = preg_replace($pattern, $replace, $img); | |
} else { | |
$pattern = '/mysite.com.au/'; | |
$result = preg_replace($pattern, $replace, $img); | |
} | |
return $result; | |
} | |
} | |
} | |
/*-----------------------------------------------------------------------------------*/ | |
/* Return replacement image src values | |
/*-----------------------------------------------------------------------------------*/ | |
if (!function_exists('ic_set_image_src')) { | |
function ic_set_image_src() { | |
$host = $_SERVER['HTTP_HOST']; | |
switch ($host) { | |
case 'localhost': | |
case 'mysite.com.au': | |
$str = $_SERVER['HTTP_HOST'].'/mysite'; | |
break; | |
default: | |
$str = $_SERVER['HTTP_HOST']; | |
} | |
return $str; | |
} | |
} | |
/*-----------------------------------------------------------------------------------*/ | |
/* Replace images with the correct sizes in single posts | |
/*-----------------------------------------------------------------------------------*/ | |
if (!function_exists('ic_resize_images')) { | |
function ic_resize_images($content = null, $after = null) { | |
$result = null; | |
preg_match_all('|<img .*?src=[\'"](.*?)[\'"].*?/>|i', $content, $assets); | |
if (sizeof($assets[0]) > 0) { | |
foreach ($assets[1] as $key=>$item) { | |
$assets[1][$key] = ic_replace_image_src($item); | |
} | |
if (count($assets[0]) >= 1) { | |
$images = array(); | |
foreach ($assets[0] as $key => $img) { | |
$images[$key] = 'img src="'.PARENT_URL.'/vendors/timthumb.php?src='.$assets[1][$key].'&w=540&zc=1" alt="'.get_the_title().'" /'; | |
} | |
$result = preg_replace($assets[0], $images, $content); | |
} | |
if (isset($after)) { | |
$result = str_replace ('/></a>', '/></a>'.$after, $result); | |
} | |
$result = apply_filters('the_content', $result); | |
$result = str_replace(']]>', ']]>', $result); | |
} else { | |
$result = $content; | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment