Posted By

krike on 10/28/10


Tagged

php wordpress script thumbnails mu timthumb


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

hkamci


Wordpress MU timthumb fix


 / Published in: PHP
 

This function will return the correct url for the timthumb php script (wordpress 3.0 or higher required!)

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)

The function: $thumb = gettimthumbthumbnail($post->ID);

I've tested this successfully on my servers so please let me know if you are having any problem with it.

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

Report this snippet  

You need to login to post a comment.