WordPress Multisite compatibility for timthumb

Ever found the necessity to use TimThumb for your WordPress theme and/or plugin? Then you should’ve faced the problem where you can not tell TimThumb about the correct location of image files in WordPress Multi-Site setup and in return TimThumb gave you internal image not found error. The tutorial over binarymoon is a bit backdated as it uses custom fields to get and set the image with TimThumb. The obvious question which arises is, how to use the WordPress’ own featured image with TimThumb to give it maximum resizing capability. Previously, we have discussed about how to get the image src from the featured image HTML and use it for other purposes like CSS etc. Now, we shall see, how to use the original image source and modify it so that it works with TimThumb (for WP MS ofcourse).

#1: Understanding the concept:

What WordPress does is, it gives some nicer URL to the static files in case of MultiSite setup. In general setup (without networking), the file URL is like this

http://yoursite.com/wp-content/uploads/2012/05/image.jpg

Which is just the absolute URL of the file.

But, for WordPress it becomes like this

http://sub.yoursite.com/files/2012/05/image.jpg

Which is obviously not the absolute static URL of the file. WordPress internally modifies the path via some htaccess rewrite to push the original content at this URL.

The main reason why TimThumb doesn’t work is, because, there is no real directory files anywhere on the root of your site. So, we have to either rewrite the URL before passing it to TimThumb or we have to somehow tell TimThumb about this issue. The second solution is not feasible as it involves editing the core of the script, which means, you have to do it again after any update. So, we shall use the first technique within our themes or plugins to make it WP MS compatible. Also, we shall explicitly check the image URL for the presence of /files/ (uploaded media) and /wp-content/ (theme or plugin files) and will treat them properly.

#2: The code behind the compatibility:

/**
 * TimThumb Image
 *
 * **Properly** appends the source image URL to timthumb for WP MS compatibility
 * Explicitly check the presence of /files/ and /wp-content/ in the URI and treats accordingly.
 * Has compatibility for both uploaded media as well as theme/plugin media files.
 *
 * You can also set height and width using the parameters
 *
 * @author Swashata
 * @param String $src The URL of the original image 'http://path/to/image.jpg
 * @param Int $h The height of the image
 * @param Int $w The width of the image
 * @return String The modified timthumb URL of the image
 */
function itg_timthumb_image($src, $h = 50, $w = 50) {
    //set your timthumb url
    $timthumb_url = get_bloginfo('wpurl') . '/path/to/timthumb.php';
    //check for multisite
    if(is_multisite()) {
        //get rid of the www part from the HTTP_HOST
        $host = preg_replace('/^www\./i', '', $_SERVER['HTTP_HOST']);
        if(preg_match('/https?:\/\/(?:www\.)?' . $host . '/i', $src)) { //check if internal
            //is it a request for directory wp-content?
            if(preg_match('/\/wp-content\//', $src)) {
                $src_parts = explode('/wp-content/', $src);
                $src = '/wp-content/' . $src_parts[1];
            }
            //is it a request for /files/ virtual directory
            else if(preg_match('/\/files\//', $src)) {
                $src_parts = explode('/files/', $src);
                $src = '/blogs.dir/' . get_current_blog_id() . '/files/' . $src_parts[1];
            }
        }
    }
    return $timthumb_url . '?src=' . urlencode($src) . '&h=' . $h . '&w=' . $w . '&zc=1';
}

And finally you call the function to do what you want. Note that you do need to pass the raw image URL as the $src variable. If you wish to TimThumb your featured images, then use the function used here and call the above function like this:

//get 200 X 200 thumbnail for the current post featured image
itg_timthumb_image(itg_get_the_post_thumbnail_url(), 200, 200);

So that was the most complete and simple solution I could think of while making my WordPress things compatible with MultiSite and TimThumb. If you know any better technique or have any problem using this, then do let us know.

5 comments

  1. Dean

    Thanks Swashata – I finally got timthumb to work on my multipress site!!! You are a genius!!! I have been crawling the web looking for solutions and every one I tried failed. Yours is the only one that has worked… very well done.
    I placed your two functions in functions.php and wherever timthumb was called I set the image src to ” />.
    Keep up the good work.

  2. Dean

    ooops – code was stripped out…..

    I set the image src to
    “” />.
    Keep up the good work.

  3. Dean

    Thanks Swashata – I finally got timthumb to work on my multipress site!!! You are a genius!!! I have been crawling the web looking for solutions and every one I tried failed. Yours is the only one that has worked… very well done.
    I placed your two functions in functions.php and wherever timthumb was called I set the image src to;
    <blockquote cite"<img src="” />
    Keep up the good work.

  4. Dean

    I give up trying to explain the code I used – lets just say it worked!

    • Swashata Post author

      That is great. You can put it inside code shortcode. [ code ] (without space)

Comments are closed.