Skip to content

Instantly share code, notes, and snippets.

@thisiskylierose
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thisiskylierose/331c154f89e7ae0ca56e to your computer and use it in GitHub Desktop.
Save thisiskylierose/331c154f89e7ae0ca56e to your computer and use it in GitHub Desktop.
Wordpress functions to run image replace for timthumb
/*-----------------------------------------------------------------------------------*/
/* 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(']]>', ']]&gt;', $result);
} else {
$result = $content;
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment