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

Share

Join me, me, Eric, shawn. They're all chatting about "How to Make TimThumb Work With WordPress Multisite" below ›

Read Comments

230 Comments »

  1. Was looking for over a year for a way to grab the first image of a post in WordPress MS, no one here could help… Here is what you need to do.

    It checks for images a few different ways all in one code.

    • Thanks for the link – unfortunately it doesn’t load so I have removed it. Happy to add it back if you can tell me the correct url

  2. Hi,

    I am trying to to get a photoblog page theme, Framed (by Jinsoma) to work properly on my blogging network. The network was originally created on WPMU. The theme uses timthumb. Even after reading through the post and all of these comments I am still lost.
    Has anyone gotten this to work on a Jinsoma made theme? I also have the Arras theme installed on many of the blog sites and it works with no adjustments.
    Any help as to what line of code to put into which file would be so greatly appreciated. I am on about my 10th page theme for the photoblog and am becoming rather frustrated. The whole time the person whose site it will be is sitting and waiting for it to be ready.

    Thanks,
    Gene
    (The photoblog site is at http://www.abphotography.worldblogosphere.com )

  3. Hi there Ben! I need your awesome help. I just installed my wordpress theme called reviewit yesterday and I’m not so lucky with TimThumb. I put the cache folder located in /public_html/wp-content/themes/reviewit/lib/scripts to 777 and still doesn’t work.

    The code that you recommended to put in the function.php I have it already

    /*************************** TimThumb Image Directory ***************************/

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

    Also I found this in the function.php

    /*************************** Featured Image Sizes ***************************/

    /*add_theme_support(‘post-thumbnails’);
    get_option(‘thumbnail_crop’);
    add_image_size(‘thumbnail’, 120, 120, true);
    add_image_size(‘ghostpool_thumbnail’, 100, 130, true);
    add_image_size(‘slider-image’, 630, 344, true);
    add_image_size(‘slider-thumbnail’, 80, 65, true);
    add_image_size(‘blog-image’, 638, 238, true);
    add_image_size(‘review-box-image’, 212, 120, true);
    add_image_size(‘review-page-image’, 150, 180, true);*/

    My website url is http://www.turmentila.info/ Please give me a hand. Many thanks!

    • It doesn’t look like your theme is using timthumb.php – you should talk to the person you got the theme from for help with getting more/ different image sizes.

      • Wrong. You replied me after 1 year so that’s why you find out that there’s no timthumb in the source. I switched to another theme.

  4. I hav a site http://www.christcollegejagdalpur.net, and you can see the thumbnails are not working when I click a category from the category menu.. help me finding a solution or pls help me to remove the unwanted links and boarder that appear before the post title…

    thanks in advance.. pls replay.. I am a student of this college and I am developing this site for my college for free.. pls do help…

  5. Hi
    Please ignore my last post that didn’t work.

    I am using the plugin ‘Download Manager’ (http://www.wpdownloadmanager.com/) and it uses timthumb.php in a number of places to display images and doesn’t support WPMS.

    In one of the page templates for example is calls:-

    if($file[preview]!=”)
    $file[‘thumb’] = “”;
    else
    $file[‘thumb’] = “”;

    and in the plugins own functions.php file an example would be:-

    $data[‘thumb’] = “”;

    I’ve added the function in functions.php but how would I amend the code?

    Would really appreciate your input.

    Glennyboy

  6. Okay, so I’ve added this code to functions.php…

    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 = ‘/wp-content/blogs.dir/’ . $blog_id . ‘/files/’ . $imageParts[1];
    }
    }
    return $theImageSrc;
    }

    but that doesn’t seem to work. when I look at the slider, and look at the image info for the image that isn’t working, I see that the img source is:

    http://www.lipolitics.com/huntington/wp-content/themes/flashnews/thumb.php?src=http://www.lipolitics.com/huntington/files/2012/04/Huntington-Opens-Electric-Car-Charging-Station_11.jpg&h=210&w=310&zc=1&q=90

    Any thoughts?

  7. I was able to fix my issue by doing TWO things.

    I turned this line to true: [line 33]

    if(! defined(‘ALLOW_ALL_EXTERNAL_SITES’) ) define (‘ALLOW_ALL_EXTERNAL_SITES’, TRUE); // Less secure.

    and I commented out this line [line 212]
    //$this->src = preg_replace(‘/https?:\/\/(?:www\.)?’ . $this->myHost . ‘/i’, ”, $this->src);

    Whether or not this is an appropriate way, it is the fix I found to work after 8+ hours of troubleshooting.

    Hopefully this helps some people!

    • Thank you Jacob Raccuia, I searched per hours on the internet and tested, nothing worked. Finally I tried your solution and it works perfectly ! thanks a lot :)

    • Hours ?? I spent 3 days to find this ! And when I almost gave up and turn back to add_image_size() … So – BIG THANKS (at this moment I don’t give much damn if it’s less secure or what …)

    • Excellent suggestion, Jacob. Thank you! I think it would be better to add this to a timthumb-config.php file, rather than changing line 33:

      <?
      $ALLOWED_SITES = array (
      '’,
      );
      ?>

      This would still allow the same functionality that you suggested, but it would limit the danger by only allowing images from the intended domain.

      I do agree that it’s necessary to comment out the preg_replace line to get this to work smoothly, though. Nice catch! I wonder why this isn’t done by default?

    • I implemented your advice on a couple of my sites and was hacked via timthumb shortly thereafter. By setting ALLOW_ALL_EXTERNAL to true, you let a hacker upload php scripts from their website to your timthumb cache directory where they are able to execute the script.

      Here’s how to do this properly:

      1. Make sure that ALLOW_EXTERNAL is set to TRUE [line 32] and ALLOW_ALL_EXTERNAL is set to FALSE [line 33]

      2. Comment out [line 212]
      //$this->src = preg_replace(‘/https?:\/\/(?:www\.)?’ . $this->myHost . ‘/i’, ”, $this->src);

      3. Go to the $ALLOWED_SITES array [line 125] delete all the default sites, and add your site’s domain.

      I would also recommend downloading the Timthumb scanner plugin and installing on your root site to make sure you’ve fixed all the versions of timthumb in your wp-content folder.

      • Hi Peter – really sorry to hear you were hacked. This problem was found 1 and a half years ago and was fixed in version 2 of TimThumb. I would recommend keeping the software up to date so that you can solve these issues before they happen.

      • Hi Peter – I’m new at this and I do not understand is the file I should modify timthumb.php – functions.php or some other file? . From already thank yo

    • IT WORKS!!!! Thank you, thank you thank you. 2 months ago I spent even more days trying to fix it before giving up. Then trying again now I have spent hours trying to fix this problem.

      Don’t know how you came up with the solution, but good job! You got some skills there.

  8. I get this message, Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 20736 bytes) in /storage/content/49/146049/unitedsardinefactory.com/public_html/wp-content/themes/blacklabel/framework/timthumb.php on line 1085

    Any idea what the problem is? I run WordPress as an installation on my webhost, so I don’t think I have access to the files. Help would be so appreciated.

  9. I created a script that loads timthumb.php into wordpress, it uses multisite. it is basically a function that you can call from your wordpress template /widget/plugin

    It can be found here https://github.com/moyarich/wordpress_timthumb_multisite

    this script supports all acceptable timthumb parameters http://www.binarymoon.co.uk/2012/02/complete-timthumb-parameters-guide/

    How to use:
    1. you add the mr_timthumb function to your functions.php
    2. edit the location to the timthumb.php script , change the value for the variable $timthumblocation
    3. then call timthumb like so

    $timthumb_img_param = array(
    ‘src’ => $orig_uploaded_img,
    ‘w’=>590,
    ‘h’ => 315,
    ‘a’=>’top’
    );

    mr_timthumb($timthumb_img_param);

    • Thanks for the contribution. Personally I don’t see a need for this – TimThumb isn’t difficult to use. But perhaps others will like it.

    • If you view the image you will see the error. In this case the GD library does not exist which means TimThumb can’t work

  10. i don’t this workaround is working anymore with new installs of WPMU using WP3.5.1 – works for subdomain setup but am having issues with the subfolder setup

    anyone else notice this?

    • sorry that should have read…

      i don’t “think” this workaround is working anymore with new installs of WPMU using WP3.5.1 – works for subdomain setup but am having issues with the subfolder setup

      anyone else notice this?

  11. No. I don’t think this works now either. I’ve been trying for two days to get this to work too and it’s getting on my nerves. I’m using a theme which had timthumb embedded (Splashing Pixels ‘Mio’ so I’m kind of stuck with it)
    So many ‘fixes’ which just seem to cause other problems.
    Everything works on the main site but can’t get any images on the subfolder sites.
    Any help gratefully received

  12. Just keep getting

    Could not find the internal image you specified.

    errors even though the image is there when you grab just the image url.

  13. The new wordpress multisite is not longer using blog.dir directory so this code doesn’t work anymore with wordress uploaded images. And i can’t figure out how to fix it, any thought?

  14. Did anyone ever find a resolution to this? I’m using Userpro plugin, which uses Timthumb and I’m coming up against the same problem.

Comments are closed. Let's continue the conversation on Twitter.
css.php