Skip to content

How to Make TimThumb Work With WordPress Multisite

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 Multisite (formerly 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 multisite 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 multisite. For reference all Pro Theme Design themes, including Elemental, already have this functionality included.

I want to emphasize that you can not modify TimThumb to work with WordPress MultiSite (or mu). Changes you make for your website are unlikely to work for other peoples. Editing your theme to support TimThumb is the best way to go, and it’s surprisingly easy.

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 Multisite 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 Multisite or WordPress normal
  2. Determine if the image is on the blog domain or some other domain
  3. Work out the 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;
}

Categories

TimThumb, Web Design

Ben View All

Ben is a lifelong Nintendo fan who also likes to build websites, and develop games. He also buys way too much Lego.

There are 230 comments Join the conversation

Join the conversation

Your email address will not be published. Required fields are marked *

css.php