How to Make TimThumb Work With WordPress Multisite

TimThumb is no longer supported or maintained.
More information →

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;
}

Let me know what you think on Mastodon, or BlueSky (or Twitter X if you must).

Related Posts

04 Nov 2010

TimThumb Troubleshooting Secrets

I often get asked questions about TimThumb and why it doesn’t work in certain situations. I can generally tell what is wrong with the script within about 60 seconds of being sent a demo url. Below are my top tips...
01 Jul 2014

I No Longer Use TimThumb – Here’s What I do Instead

Last week there was a second exploit found in TimThumb. Thankfully it was no-where near as bad as the first one – but it raised an interesting question of whether TimThumb is even needed anymore.TimThumb was made to be useful...
01 Apr 2015

The State of WordPress Themes #wcldn

I recently spoke on a panel at WordCamp London 2015e. Lance – who used to be the Theme Team lead at WordPress.com – asked me if I wanted to speak on a panel with him at WordCamp London 2015. I’ve...
05 Aug 2010

Using TimThumb part 1: Getting Started

TimThumb has always been built with simplicity in mind. However there are a few things it can do that have not been exposed before.Inspired by a comment from RBhavesh I have decided to write a series of posts in which...
04 Nov 2016

Automating WordPress Development with Gulp, Bash, and PHP

When I wrote about the things I had learned from releasing 20 WordPress themes, I mentioned that I had automated as much as I could. One of the things I automated was the build and deployment process for my themes...