How to use timthumb in WP MU

I think to show the first image as thumb in wp front page or blog layout is very common for any theme. But to use it in wordpress mu version is not direct or need to do some hack or modify the normall process of adding timthub. Normally we upload images from wp media manager and use those in posts. In normal wordpress the upload dir is wp-content/uploads and files and images are uploaded to that directory. In wordpress mu version files are uploaded by user basis folder and for this wp-content/blogs.dir/userid is the uploaded dir for any image. here user id is named as folder name for each user. then wp-content/blogs.dir/userid/files. But when u browse the site the image link format is like http://yoursitename/files/year/month/image name but format is reshaped by the wp mu htacces which real physical path is like wp-content/blogs.dir/userid/files/year/month/image name.

the common way to get the first image link from a wp post to use this for timthumb is like bellow:
in wp loop

$pattern = '!preg_match_all($pattern, $post->post_content, $matches);$image_src = $matches['1'][0];

if $image_src is not null then we generally use this src for image to be used as image src for timthumb. But for wp mu this will not work as for wp mu the physical path is diff.

Sol: Use global var $blog_id like global $wpdb,$post,$blog_id;
and then

if($image_src != '')
{
   $replace = 'wp-content/blogs.dir/'.$blog_id.'/files';$image_src = str_replace('files',$replace,$image_src);

}

hope u know how to use timthumb and will get solution for using it in wp mu. may this post will remove some pain when u are here by google search about this problem
thanks

Last Update on March 24th, 2010

This entry was posted in wordpress, Wordpress Plugin, Wordpress Themes and tagged , , . Bookmark the permalink.

4 Responses to How to use timthumb in WP MU

  1. Ella says:

    Its really good.keep up the good work.

  2. Jon says:

    SO where do you paste these codes to make it work??

    Sorry, Im not very familiar with codes

  3. Chris M says:

    This worked perfectly, life saver, thank you!

  4. Bob says:

    Hey! Stupid!

    Explain for people where to put this code!

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>