Quick mod for the Post Thumbs Wordpress plugin

Working on a site redesign, I found that I needed a plugin for Wordpress that would let me associate an image with the post that would act as a thumbnail on the pages where multiple posts are listed. I found exactly what I needed in the Post Thumbs plugin.

This plugin takes the first image it finds in the post content, thumbnails it, and displays the thumbnail. Since the site I’m working on typically only has one image per page, and even if there are more the first is always the important one, this is ideal. I don’t have to do anything extra; the thumbnails are automatically created for me.

But I found one major issue. The plugin would only work from within the Wordpress loop. Normally not an issue, but I found myself needing to use the thumbnail on a few pages that did not include a loop and also on the sidebar. So I modded the function to allow you to pass a post id. If the post id is passed that id is used for the thumbnail. If the post id is not passed then the loop is used.

It’s only about 4 lines of changes. First let’s look at the first few lines of the function that actually handles the thumbnailing. This is copied directly from the plugin source file post-thumb.php and can be found starting on line 38 in the version I downloaded.

PHP:
  1. function tb_post_thumb($generate=false, $alt_text=, $resize_width = 0, $resize_height = 0, $crop_x = 0, $crop_y = 0) {
  2.    
  3.     global $post;
  4.     $settings = get_option(‘post_thumbnail_settings’);
  5.    
  6.     // find an image from your domain
  7.     if (preg_match(‘/<img (.*?)src="https?:\/\/(www\.)?’.str_replace(‘/’,\/,$settings[‘domain_name’]).\/(.*?)"/i’,$post->post_content,$matches)) {
  8.         // put matches into recognizable vars
  9.         // fix later, assumes document root will match url structure
  10.         $the_image = $settings[‘base_path’] . ‘/’ . $matches[3];

There’s a lot more there, but the changes all happen within what I copied.

The first thing we’re going to add is one more variable to the end of the function declaration. Add ‘, $post_id=0′ after ‘$crop_y’. We’re also going to add an if construct, but I’ll put that in the code below because it’s just easier to follow.

PHP:
  1. function tb_post_thumb($generate=false, $alt_text=, $resize_width = 0, $resize_height = 0, $crop_x = 0, $crop_y = 0, $post_id=0) {
  2.     global $post;
  3.     $settings = get_option(‘post_thumbnail_settings’);
  4.    
  5.     if ($post_id!=0)
  6.         {
  7.         //  Use the current post
  8.         $post = get_post($post_id);
  9.         }
  10.    
  11.     // find an image from your domain
  12.     if (preg_match(‘/<img (.*?)src="https?:\/\/(www\.)?’.str_replace(‘/’,\/,$settings[‘domain_name’]).\/(.*?)"/i’,$post->post_content,$matches)) {
  13.         // put matches into recognizable vars
  14.         // fix later, assumes document root will match url structure
  15.         $the_image = $settings[‘base_path’] . ‘/’ . $matches[3];

Line 1 has the additional variable in it. The only other addtion is lines 5 through 9. All that’s done is that if 0 is not passed as the post id then the variable $post is loaded using the Wordpress function get_post.

As a side note, it’s always nice to go out and find an author that’s shared a plugin that’s almost perfect for your needs.

Question, Comments...

Do you have more questions. Please either leave a comment below or join us in our new forum.

Leave a Reply