WordPress MU timthumb fix – code snippet

by krike in Code snippets / Freebees on 02 Nov 2010

A free code snippet in order to make the timthumb script work on you wordpress MU installation which seems to be a big problem on most blogs.

In one of my latest projects, creating a wordpress premium theme, I need to use the timthumb php class which would properly resize the post thumbnails. It worked perfectly untill I added the theme to the demo which is running wordpress MU (multi user).

The problem is that WordPress MU rewrites the url to the images and therefore timthumb can’t find the images. When I googled for a solution I noticed that I wasn’t the only one having this problem. I tried different solutions but none of them worked so I took a piece of code that was pretty simple and close to the solution and I improved it.

The following function for wordpress should be added in the functions.php of your theme, more instructions can be found in the code snippet itself. If you have any question at all you can always leave a comment below, enjoy! :)

  1. /*
  2.         WORDPRESS MU TIMTHUMB FIX
  3.         This function will return the correct url for the timthumb php script (wordpress 3.0 or higher required!)
  4.        
  5.         Usage: just call the function in your theme with the post id as parameter, it will return a value so store this into a variable (must be inside the wordpress loop to work)
  6.        
  7.         The function:
  8.         $thumb = get_timthumb_thumbnail($post->ID);
  9. */
  10. function get_timthumb_thumbnail($post_id = null) {
  11.         global $blog_id;
  12.        
  13.         //we can access it, so why not use it?
  14.         $is_mu = WP_ALLOW_MULTISITE;
  15.        
  16.         //if is true it means it’s a wordpress MU site and we’ll have to do some work
  17.         if($is_mu == true):
  18.                 $thumbnail_id=get_the_post_thumbnail($post_id);
  19.                 preg_match (‘/src="(.*)" class/’,$thumbnail_id,$link);
  20.                 $imageParts = explode(‘files/’, $link[1]);
  21.                 if (!empty($imageParts[1])):
  22.                         //check if the image is in the blog directory
  23.                         if(@getimagesize(‘./wp-content/blogs.dir/’ . $blog_id . ‘/files/’ . $imageParts[1])):
  24.                                
  25.                                 $thumbnail = ‘http://’.$_SERVER[‘HTTP_HOST’].‘/wp-content/blogs.dir/’ . $blog_id . ‘/files/’ . $imageParts[1];
  26.                        
  27.                         endif;
  28.                         //check if the image is in the main uploads directory (you never know)
  29.                         if(@getimagesize(‘./wp-content/uploads/’ . $imageParts[1])):
  30.                                
  31.                                 $thumbnail = ‘http://’.$_SERVER[‘HTTP_HOST’].‘/wp-content/uploads/’ . $imageParts[1];
  32.                        
  33.                         endif;
  34.                     else:
  35.                             $imageParts = explode(‘uploads/’, $link[1]);
  36.                             if(@getimagesize(‘./wp-content/uploads/’ . $imageParts[1])):
  37.                                 $thumbnail = ‘http://’.$_SERVER[‘HTTP_HOST’].‘/wp-content/uploads/’ . $imageParts[1];
  38.                             endif;
  39.                 endif;
  40.  
  41.         else:
  42.                 $thumbnail_id=get_the_post_thumbnail($post_id);
  43.                         preg_match (‘/src="(.*)" class/’,$thumbnail_id,$link);
  44.                         $thumbnail = $link[1];
  45.         endif;
  46.  
  47.         return $thumbnail;
  48. }

Author:

Christophe (aka krike) is a Web designer & developer living in Belgium. He spends most of his time working with Wordpress, CodeIgniter & Photoshop but also helping beginners take their first step in the development world.

3 Comments

  1. ste on 06 Feb 2011

    Hi Krike,
    it doesn’t work for me, I’m telling the change I’ve made in the code:
    if(@getimagesize(‘./wp-content/blogs.dir/4/’ . $blog_id . ‘/files/’ . $imageParts[1])):

    $thumbnail = ‘http://'.$_SERVER'HTTP_HOST'.'/wp-content/blogs.dir/4/‘ . $blog_id . ‘/files/’ . $imageParts[1];

    what’s wronk in your opinion

  2. krike on 06 Feb 2011

    @ste I can’t tell you without having a look at your site, can you give me a link? i’ll be able to help you better.

  3. ste on 07 Feb 2011

    is http://www.textilearchitecture.polimi.it/research/

Drop us a word

Spam Protection by WP-SpamFree