Home/ Journal Bens Projects Binary Moon Archives About Ben Gillbanks Subscribe to Binary Moon Updates

Subscribe to Binary Moon Website Updates, it's Free and Easy to Stay in Touch

Search Binary Moon

Search Binary Moon

How to Make TimThumb Work With WordPress mu

tim-thumb-1TimThumb is a popular image resizing script that was created for Mimbo Pro - but it's never worked properly with WordPress MU, so I wanted to change that.

The reason it doesn't work is quite simple. Because of the way WordPress MU runs multiple blogs it uses a server rewrite rule to point to media uploads. This in turn means that the file paths for the images do not actually exist in the way they are used in blog posts.

People have asked me how to make TimThumb work with mu quite a few times, but now I've looked into it I have concluded that TimThumb doesn't need changing - it does what it's told to (resize images). What needs to change is the file path of the media being resized - which means we need to alter our themes to work with MU. For reference all Pro Theme Design themes, including Elemental, already have this functionality included.

When using TimThumb I would recommend using the latest version of the plugin, and passing it the absolute url of the image you want to resize - however for WordPress MU the path on the server does not match the website path, and you need to take this into account.

The first thing to do is create a function to grab the image path (and not just echoing a get_post_meta value). This will make future changes easier.

We should then do a series of things to get the correct file path for the image.

  1. Determine if the theme is being used on WordPress mu or WordPress normal
  2. Determine if the image is on the blog domain or some other domain
  3. Work out the WPmu blog ID
  4. Calculate the file location based on the gathered info

Which turns into code that looks something like this:

$theImageSrc = 'path/To/Image.jpg';
global $blog_id;
if (isset($blog_id) && $blog_id > 0) {
	$imageParts = explode('/files/', $theImageSrc);
	if (isset($imageParts[1])) {
		$theImageSrc = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
	}
}

Pulling this together

Since most themes use a custom field to store the thumbnail image path a PHP function to calculate the image location would look something like this:

function get_image_path ($post_id = null) {
	if ($post_id == null) {
		global $post;
		$post_id = $post->ID;
	}
	$theImageSrc = get_post_meta($post_id, 'Image', true);
	global $blog_id;
	if (isset($blog_id) && $blog_id > 0) {
		$imageParts = explode('/files/', $theImageSrc);
		if (isset($imageParts[1])) {
			$theImageSrc = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
		}
	}
	return $theImageSrc;
}

90 Responses to “How to Make TimThumb Work With WordPress mu” Leave a reply ›

  • Hello,

    does anybody use this hack into eleganttheme template AND wordpress 3 multisite? I have big problem to integrate with ePhoto from eleganttheme - could anybody help me with this?

    regards,
    Daniel

Leave a Reply

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>

About me

About BenMy name is Ben Gillbanks. I'm a lover of Video Games, WordPress, Web Development and everything in between.

I have been working on the internet since 1998, and working with computers even longer. I am a hardcore Nintendo fanboy and have owned most of their consoles at one stage or another.

Read more about me on my about page.


Follow Me


Random Link-outs

The Binary Network links to all my websites
bengillbanks.co.uk - Ben Gillbanks

Ben Gillbanks

All my websites under 1 roof, the easiest way to find out what I do

Pro Theme Design - premium WordPress themes

Pro Theme Design

Premium WordPress themes by web design pros (erm... that includes me)

Binary Joy - gaming news and reviews

Binary Joy

Gaming news and reviews

Binary Sun - play free online games

Binary Sun

Play and download free and paid games (many made by me)

Gaming Angel - download and play games online

Gaming Angel

Stacks of shareware games, free to try and cheap to buy