Home » Wordpress

Making TimThumb.php work with WPMU

28 August 2008 28,912 views 11 Comments

It has taken almost 16 hours dissecting the wpmu system after being semi familiar with the basic wordpress setup. There aren’t nearly as many people who use wpmu as opposed to wp so troubleshooting becomes tough and you begin to rely on yourself to figure it out. I’ve done search across the net for hours seeing if there is a solution, but to no avail. Now that I’ve figured it out I have to pass it on.

First off, if you’ve made any changes to the timthumb.php script, you’re on your own. So I’m going to start with the original script which you can download from Darren Hoyt‘s blog. I created a folder inside my theme called scripts. Inside scripts we have timthumb.php and another folder called cache set to chmod 0777 permissions. Some people have told me you need to set the timthumb.php to 0777 but I have found that to be untrue as it doesn’t matter AND creates a security flaw. I recommend keeping the script at 0755 (default).

Start by making sure the script works. To do this access the file directly with no variables set. It should give you an error with ‘No Image Specified’. Here is an example:

http://sheaallen.com/wp-content/themes/arthemia/scripts/timthumb.php

This lets us know the script is working no problem. According to the creator’s instructions, we are told to use the following to call an image:

<img src="/scripts/timthumb.php?src=/images/whatever.jpg&h=150&w=150&zc=1" alt="" />

The idea is to add a custom value field named “Image” (with a capital i – there is a difference between image and Image) with the value pointing to the location of your image. In my case, when I upload an image using wordpress it puts it in the following place:

http://www.sheaallen.com/files/2008/08/file.gif

If you use this path in your image src using wpmu you’ll get the following error even though the image IS there:

//files/2008/08/file.gif not found.

Reason for this happening is because in the default .htaccess for wpmu, the modrewrite article is used to change the /files/ to wp-content/blogs.php?file=location. In return, the blogs.php file actually pulls the image from a totally seperate directory. In my case, it is blog 1. So the image is actually located

http://www.sheaallen.com/wp-content/blogs.dir/1/files/2008/08/file.gif

I’m not a programmer, i just learn what i need to know to make things work. So i cannot tell you why all these rewrites are causing the image to not display. Anyways, for the fix. In the code of your template you must put this:

<img src = scripts/timthumb.php?src=http://www.sheaallen.com/wp-content/blogs.dir/1/<?php $values = get_post_custom_values(“Image”); echo $values[0]; ?>&w=300&h=275&zc=1&q=100>

In my case, the custom Image value would be files/2008/08/file.gif (no slash at the beginning). Yes, there may be other ways to do this, possibly something simpler and easier on the end user, but this method DOES work and it currently doesn’t have any flaws. If you have another way to make this fix, please let me know. Once again, I’m not a programmer but I’ll attempt to help you if it still doesn’t work. Good luck!

UPDATE 3/7/2010!
This code will work better than hardcoding your web address and blog directory as shown above:

<?php bloginfo(‘template_url’);?>/scripts/timthumb.php?w=203px&h=138&src=<?php bloginfo(‘url’); ?>/wp-content/blogs.dir/<?php echo $wpdb->blogid; ?>/<?php $values = get_post_custom_values(”Image”); echo $values[0]; ?>&zc=1&q=100

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 3.00 out of 5)
Loading ... Loading ...

11 Comments »

  • Drew Geraets said:

    Thanks for posting this. The only thing I have to add is a way to automatically generate the blog ID, instead of hard-coding that.

  • KD said:

    I may as well join the club I am having the same problem view images on my site. I made some changes that you have suggested but I may have lost a step when you used /wp-content/blogs/1/ , I interpreted that as /wp-content/themes/blogname/. Here is my script, I’ve been trying to be as careful as possible, could you take a look and lend me a hand if possible.

    <a href=”" rel=”bookmark” title=”Permanent Link to “>
    <img src=http://www.hollaatyoboy.com/hwp/wp-content/themes/arthemia/scripts/timthumb.php?src=/wp-content/uploads/&w=300&h=275&zc=1&q=100″
    alt=”" class=”left” width=”300px” height=”275px” />

    <a href=”" rel=”bookmark” title=”Permanent Link to “>Read the full story »

  • Scorpion said:

    <img src = scripts/timthumb.php?src=http://www.sheaallen.com/wp-content/blogs.dir/1/&w=300&h=275&zc=1&q=100>

    The only trouble with that code is, it’s only going to work for a singel user. The next user, whose media file will be stored in blogs.dir/2 will have bigger problems since the blogs.dir/1 is hard coded into the template

  • Shea Allen (author) said:

    @ Scorpion – you are correct. It will only work for blog #1. However, as Drew mentioned up there you can automatically generate the blog ID using the $blog_id function.

    Updated you could use this:

    <img src = scripts/timthumb.php?src=http://www.sheaallen.com/wp-content/blogs.dir//&w=300&h=275&zc=1&q=100>

  • shawn said:

    I think I am getting close using your solution, but still have an issue. I am using 2.7.1 wordpress mu

    here is the header template code I am using to change the thumbnail

    <img src="/scripts/timthumb.php?src=h t t p://www.mywebsite.com/wp-content/blogs.dir//&h=195&w=540&zc=1&q=95" alt="" />

    This results in a link like:

    http://mywebsite.com/wp-content/themes/arthemia-premium/scripts/timthumb.php?src=/wp-content/blogs.dir/8/h t t p://mywebsite.com/files/2009/05/imagetest.jpg&w=200&h=225&zc=1&q=100

    now if I could get it to remove the h t t p://mywebsite.com from the second part of the url, after /8/ it would be perfect, but for some reason I can’t seem to get it to remove that part.
    (***h t t p does not have spaces, I have to do that to get it by your comment spam as it wont allow it without spaces)

    What do you think I may be missing?

  • shawn said:

    arrgghhh

    even wrapped in code tags, it is removing what I put in there above….

    Let me try this again with some more spaces in the code area

    I think I am getting close using your solution, but still have an issue. I am using 2.7.1 wordpress mu

    here is the header template code I am using to change the thumbnail

    <img src="/scripts/timthumb.php?src=h t t p://www.mywebsite.com/wp-content/blogs.dir//&h=195&w=540&zc=1&q=95" alt="" />

    This results in a link like:

    http://mywebsite.com/wp-content/themes/arthemia-premium/scripts/timthumb.php?src=/wp-content/blogs.dir/8/h t t p://mywebsite.com/files/2009/05/imagetest.jpg&w=200&h=225&zc=1&q=100

    now if I could get it to remove the h t t p://mywebsite.com from the second part of the url, after /8/ it would be perfect, but for some reason I can’t seem to get it to remove that part.
    (***h t t p does not have spaces, I have to do that to get it by your comment spam as it wont allow it without spaces)

    What do you think I may be missing?

  • shawn said:

    ok I give up, I put spaces before the php portions of the code, wrapped it in code tags and your system is still removing it. No idea what I need to do in order to paste it in here for you to review.

    As my comments are useless without it, feel free to just delete them.

  • Shea Allen (author) said:

    @shawn – your problem looks like it lays where you put in your custom image value. It appears that you’re putting the full length of the file in it when it should only pick up at files/

    Fore example: On your last one you have a result that looks like:
    /wp-content/blogs.dir/8/http://mywebsite.com/files/2009/05/imagetest.jpg

    So according to me, everything would work if the http://mywebsite.com was removed. The only place this would come into play is in the actual content you’re feeding the custom image value. That value should change to files/2009/05/imagetest.jpg THEN you’ll have this result:

    /wp-content/blogs.dir/8/files/2009/05/imagetest.jpg which is the correct location.

  • shawn said:

    I think part of the problem is I am not defining the thumbnail image via custom fields, but rather letting arthemia choose the first image.

    The problem I see with that, is there is no way for me to remove the url portion of the link.

    Are you able to simply let arthemia choose the first image, or do you specify the image via custom field Image?

    btw
    I did get your emails, thank you very much.
    I tried the latest timthumb file you sent, but it made no difference. I’m guessing it’s either the issue I mention above, or I still have something setup wrong.

    I don’t plan on giving up though, as I like the theme.

  • Ben said:

    Sorry guys I’m pretty confused. Can you please just tell me which files I will need to alter, and what code. I want to make this work site wide.

  • Jitesh said:

    Hi Shea Allen…after enabling wordpress multisite i started finding codes to fix TimThumb, but none of them worked. Then i found out a simple solution. The solution was to rid away TimThumb and add a simple code by replacing the TimThumb code in functions.php

    <a href="” rel=”bookmark”><img src="http://yoursite.com/wp-content/blogs.dir/2/ID, ‘full’); ?>” alt=”" />

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

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

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.