How to make TimThumb work with single site and multisite

For those who doesn’t know what timthumb are. Check out the most popular image crop php used in WordPress.

TimThumb were no doubt used by many popular free themes and premium themes but in some cases timthumb did not work in your multisite and network site.

Here’s some step to make timthumb work with single site and network multisite WordPress installation.

First – create a timthumb-config.php in the same directory of timthumb.php

Try not to edit the timthumb.php so updates on the script can be consistent without you having to edit the code again when new patch or update to timthumb released.

in timthumb-config.php add this code

<?php
if( is_file( '../../../../../wp-config.php' ) ) {
require_once('../../../../../wp-config.php');
} else {
require_once('../../../../wp-config.php');
}
define ('ALLOW_EXTERNAL', FALSE);
$tim_uploads = wp_upload_dir();
$tim_upload_path = $tim_uploads['basedir'] . '/cache/';
$tim_upload_path_check = $tim_uploads['basedir'] . '/cache';
//Create the upload directory with the right permissions if it doesn't exist
if( !is_dir( $tim_upload_path_check ) ){
mkdir($tim_upload_path_check, 0777);
chmod($tim_upload_path_check, 0777);
}
define ( 'FILE_CACHE_DIRECTORY', $tim_upload_path_check );
?>

This will allowed timthumb to create a cache directory in wp-content/uploads for single site and wp-content/blogs.dir/BLOG_ID/files in multisite. This way all the cache/temp file will not be saved inside the themes directory.

Second – create a function to fetch the images

Please note: in this case, my timthumb.php were inside lib/timthumb/timthumb.php however your timthumb location and default images maybe different from the functions code.

copy paste this code to your functions.php

if( !function_exists( 'get_featured_post_image' )):
function get_featured_post_image($width, $height, $class, $title) {
global $blog_id,$wpdb, $post, $posts;
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'large');
$image_url = $image_url[0];

if(!$image_url){

$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0]; ?>

<?php if($first_img) {

if( !is_multisite() ) {
return "<img class='" . $class . "' src='" . get_template_directory_uri() . "/lib/timthumb/timthumb.php?src=" . $first_img . "&amp;h=" . $height . "&amp;w=" . $width . "&amp;zc=1&amp;a=tl' alt='' title='" . $title . "' />";
 } else {
return "<img class='" . $class . "' src='" . get_template_directory_uri() . "/lib/timthumb/timthumb.php?src=" . get_current_site(1)->path . str_replace( get_blog_option( $blog_id,'fileupload_url'),get_blog_option($blog_id,'upload_path'), $first_img) . "&amp;h=" . $height . "&amp;w=" . $width . "&amp;zc=1&amp;a=tl' alt='' title='" . $title . "' />";
 }
 } else {
 return "<img class='" . $class . "' src='" . get_template_directory_uri() . "/lib/timthumb/timthumb.php?src=" . get_template_directory_uri() . '/lib/styles/images/feat-default.jpg' . "&amp;h=" . $height . "&amp;w=" . $width . "&amp;zc=1&amp;a=tl' alt='' title='" . $title . "' />";
 }

} else {

if( !is_multisite() ) {
return "<img class='" . $class . "' src='" . get_template_directory_uri() . "/lib/timthumb/timthumb.php?src=" . $image_url . "&amp;h=" . $height . "&amp;w=" . $width . "&amp;zc=1&amp;a=tl' alt='' title='" . $title . "' />";
 } else {
return "<img class='" . $class . "' src='" . get_template_directory_uri() . "/lib/timthumb/timthumb.php?src=" . get_current_site(1)->path . str_replace( get_blog_option( $blog_id,'fileupload_url'),get_blog_option($blog_id,'upload_path'), $image_url) . "&amp;h=" . $height . "&amp;w=" . $width . "&amp;zc=1&amp;a=tl' alt='' title='" . $title . "' />";
 }
}
}
endif;

Parameter Explaination

$width = desired width for timthumb to cropped
$height = desired height for timthumb to cropped
$class = add class to the image - alignleft, aligncenter or any custom class
$title = for title

Timthumb will first check if the post had WordPress post_thumbnail exist, if not the function will regex or get the first internal images from the post *external image will not be fetch if both post_thumbnail and get first images failed, default images will be used instead.

Third – how to use the new function in template

just add this code to any location you want the featured timthumb cropped image to show.

only cropped image

<?php echo get_featured_post_image(150, 150, 'alignleft', get_the_title()); ?>

cropped image with link

<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php echo get_featured_post_image(150, 150, 'alignleft', get_the_title()); ?>
</a>

that’s all folks, let me know if any improvement to the code can be done. Code wise are always welcome.

Thanks

http://www.magpress.com/wp-content/uploads/2012/06/06292012_094545.pnghttp://www.magpress.com/wp-content/uploads/2012/06/06292012_094545-150x150.pngRonald For those who doesn't know what timthumb are. Check out the most popular image crop php used in WordPress. TimThumb were no doubt used by many popular free themes and premium themes but in some cases timthumb did not work in your multisite and network site. Here's some step to make... MagPress

2 Comments Already

  1. In the third step (how to use the new function in template)

    You say “add this code to any location you want the featured timthumb cropped image to show.”
    Where should I go ?
    Can you give a exemple of the location and file where we should add this code ?
    Thank

    • yes it should be inside post the_loop() like this

      <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
      <?php echo get_featured_post_image(150, 150, 'alignleft', get_the_title()); ?>
      </a>
      <?php the_content(); ?>

Leave a Reply

Please note: Do not SPAM this comment form, all links will be delete immediately