Using TimThumb with WordPress MU

I was recently working on a theme for WordPress MU that needed an image set in the theme options to display underneath the header. This was no problem at first, but when I tried to resize it using TimThumb the image stopped appearing. A quick google search took me to Binary Moon, where it’s explained how WordPress MU treats image paths differently than a standard WordPress install. There was also the following snippet of code that fixes this issue.

Since I know I’ll need this code in the future, I’m posting it again here.

Function to Get Correct Image Path for WordPress MU

1
2
3
4
5
6
7
8
9
10
11
12
/* MU Specific Function to Determine Actual Image Path */
 
function get_mu_image_path ($img_src) {
    global $blog_id;
	if (isset($blog_id) && $blog_id > 0) {
		$imageParts = explode('/files/', $img_src);
		if (isset($imageParts[1])) {
			$img_src = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
		}
	}
	return $img_src;
}

Sample Code That Displays TimThumb Image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Add the media box
 
function childtheme_media_box() {
  //This gets the image url that I had set in the theme options
  $mediabox = get_option('childtheme_mediabox');
    if(!empty($mediabox)) {
      //This calls the function we just defined above to get the correct image path
      $mediabox = get_mu_image_path($mediabox);?>
      <div id="mediabox">
      //This displays the image using the TimThumb script
      <img src="<?php bloginfo('stylesheet_directory'); ?>/scripts/timthumb.php?src=<?php echo $mediabox; ?>&amp;w=960&amp;zc=1" alt="Media Box Image" />
      </div>
<?php }
}
 
//This is a Thematic filter that will place this html in the correct spot in the theme
add_action('thematic_belowheader','childtheme_media_box',1);
This entry was posted in WordPress Tips. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted December 5, 2009 at 2:11 pm | Permalink

    Hey there – nice snippet :)

    I’m glad the code was useful for you. Let me know if you find any optimisations/ changes/ improvements with the code.

One Trackback

  1. [...] Luckily, I stumbled on a post by another wordpress aficionado and Thematic contributor Devin Price, here, that gave me the final piece of the puzzle I needed to put this issue to [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting