Twitter
Tutorial: WordPress
13. July 2009 · Author: Dale Crum
6 Comments

WordPress: Provide a Better Naming Convention for Thumbnails

We never really brought the WordPress thumbnail feature to its full potential until recently. Initially, we assumed that WordPress provided a codex tag that would allow for the easy insertion of a thumbnail image, keeping it separate from the full-size image, something such as wp_thumbnail would have been perfect. What we discovered was that WordPress has no such tag or simple naming convention set for the use of thumbnails. This isn’t to say WordPress hasn’t thought about this at all.

What WordPress does provide are a few options for basic thumbnail insertion. The first option is to simply add a link to the thumbnail image ( http://www.mysite.com/wp-content/uploads/myImage-125×125.jpg ). The second option is to utilize a WordPress / Flutter Custom Field with the thumbnail file name followed by the dimensions provided under Settings > Media. WordPress 2.7 + uses the thumbnail format “myImage-125×230.jpg” assuming the image sizes never change, we could set up this Custom Field for the file name only: ( myImage ) though if the images ever change size, this could be a small disaster. The third option is to use a plugin such as Flexible Upload to adjust the thumbnail sizes on-the-fly. Unfortunately, Flexible Upload is only compatible up to WordPress 2.5.

But why go through all of this trouble when WordPress auto generates a thumbnail for us? Shouldn’t we be able to include a simple line of code to insert the thumbnail with such ease that if I change the thumbnail dimensions I won’t need to alter the file names a second time? What happens when WordPress changes the naming convention back to “myImage-thumbnail.jpg” and we have a thousand Custom Fields using the latest dimensional convention?

If WordPress would implement an option that would allow the user to choose their own naming convention, this wouldn’t be a problem. We really didn’t have time to wait for WordPress so we hacked the core and designed our own thumbnail prefix. This allows us to avoid the need for text-editor inserted images, Custom Fields and random plugins.

By creating a prefix to the file name as opposed to the suffix that WordPress provides, we open the door for thumbnails to be free of their bonds. The typical “myImage-125×230.jpg” becomes “thumbnail-myImage.jpg”. Now we can keep all thumbnail images grouped in the uploads folder and use the original, full-size image Custom Field to insert the thumbnail into the post. What we get is a two-for-one Custom Field.

Using only a single Custom Field in the post area we can call on the full-size image with the following:

<img src="<?php bloginfo('url'); ?>/wp-content/uploads/<?php echo get_post_meta($post->ID, 'full-image', $single=true) ?>">

and calling on the thumbnail only requires a small modification to this code:

<img src="php bloginfo('url'); ?>/wp-content/uploads/thumbnail-php echo get_post_meta($post->ID, 'full-image', $single=true) ?>">

Note the inclusion of “thumbnail-” as a prefix allowing us the control we need to access the file.

In order to set this up, a few minor adjustments to the core file “media.php” is required. The “media.php” file is located in the wp-includes folder. This hack is directed toward users of WordPress 2.7 + so it may be necessary to locate these lines through a search feature. The first adjustment is located on line 358:

if ( !$suffix )
$suffix = "{$dst_w}x{$dst_h}";

Replace {$dst_w}x{$dst_h} ( the instructions telling WordPress to name the the thumbnail file with the dimensions provided under Settings > Media ) with whatever prefix is desired. We chose “thumbnail-” for simplicities sake. The result is below:

if ( !$suffix )
$suffix = "thumbnail";

Next it is important to remember this is still a suffix which means WordPress is using it at the end of the file name. We want to create a prefix to appear before the file name. This is achieved by altering the two additional lines of code.

alter: $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
to: $destfilename = "{$dir}/{$suffix}-{$name}.{$ext}";
alter: $destfilename = "{$dir}/{$name}-{$suffix}.jpg";
to: $destfilename = "{$dir}/{$suffix}-{$name}.jpg";

All that was done here was moving the {$suffix} to the front of the file name {$name}, essentially making it a prefix. While technically we left the code named “suffix”, it has become a prefix because of it’s new location. All that is left is to save the file, upload it to wp-includes and you’re ready to enter a new world of thumbnail freedom, providing a much easier way to keep WordPress in check.

Please remember that this is not a plugin therefore, when upgrading, this core hack must be performed again.

6 Comments

  1. Mattias,

    Nice work, now we need to bundle this into a plugin.

  2. Actually, I made a much more simpler alteration.

    Instead of changing a few files like my last post, you just need to change one file and that is wp-includes/media.php

    row 434
    old code

    
      // $suffix will be appended to the destination filename, just before the extension
      if ( !$suffix )
        $suffix = "{$dst_w}x{$dst_h}";
    

    new code

    
      // $suffix will be appended to the destination filename, just before the extension
      if ( !$suffix ){
        //thumbnail
        $max_width = intval(get_option('thumbnail_size_w'));
        $max_height = intval(get_option('thumbnail_size_h'));
        
        if($dst_w == $max_width || $dst_h == $max_height)
          $suffix = 'thumbnail';
        
        //medium
        $max_width = intval(get_option('medium_size_w'));
        $max_height = intval(get_option('medium_size_h'));
        
        if($dst_w == $max_width || $dst_h == $max_height)
          $suffix = 'medium';
        
        //large
        $max_width = intval(get_option('large_size_w'));
        $max_height = intval(get_option('large_size_h'));
        
        if($dst_w == $max_width || $dst_h == $max_height)
          $suffix = 'large';
        
      }
    

    This code will check with the values you put on the various sizes and if there is a hit, the suffix will change accordingly. I haven’t tested it so much so if someone sees a mistake somewhere or can apply with a better solution, please do.

  3. Hi.

    I think I’ve managed to solve the isseu where you want the naming to be image-thumbnail.jpg, image-medium.jpg and image-large.jpg. If someone else have another good idea, please do tell.

    The following was done with some core files (wordpress 3.1.1) :

    in file wp-admin/includes/image.php

    row 107
    old code:

    
    $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE );
    

    new code
    
    $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE, 'image_size' => $s );
    

    row 125
    old code

    
    $resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] );
    

    new code
    
    $resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'], $size_data['image_size'] );
    

    in file wp-includes/media.php

    row 406
    old code

    
    function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
    

    new code
    
    function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90, $image_size = null ) {
    

    row 434-435
    old code

    
      if ( !$suffix )
        $suffix = "{$dst_w}x{$dst_h}";
    

    new code
    
      if ( !$suffix ){
        if($image_size != null)
          $suffix = $image_size;
        else
          $suffix = "{$dst_w}x{$dst_h}";
      }
    

    row 484 (or maybe higher because of alteration of code above)
    old code

    
    function image_make_intermediate_size($file, $width, $height, $crop=false) {
    

    new code
    
    function image_make_intermediate_size($file, $width, $height, $crop=false, $image_size = null) {
    

    row 486
    old code

    
    $resized_file = image_resize($file, $width, $height, $crop);
    

    new code
    
    $resized_file = image_resize($file, $width, $height, $crop, $image_size);
    

  4. Yes, that is whats happening. Both small and medium are being renamed to thb-imagename > hence, the small size gets replaced with the same naming ‘medium’ file :( so i am now getting one less image size than i should

  5. Anja,

    Good point. This scenario doesn’t take into consideration alternate image sizes. We will look into this.

  6. This is great ! ALMOST exactly what I needed. Problem is… what happens with medium sized images? Will they be called -thumbnail, too? I am looking for a way to do the same for medium sized images so that I will have myimage-medium.jpg and myimage-thumbnail.jpg in the end!
    Seems this will get me one step further … thank you for that. Great work!

Trackbacks

  1. wp-popular.com » Blog Archive » WordPress: Provide a Better Naming Convention for Thumbnails | Doc4
  2. Descubrimientos del 25 Marzo 2010 | Blog de unique3w
  3. Twitted by ameliajp
  4. Better Thumbnail Naming Convention: Control Your Thumbnails - WordPress Forums

Leave A Comment

Please wrap any code within the <code> tag to display properly

Multicons is a multi-favicon code generator which automatically inserts the necessary meta tags for both favicons (site-wide and/or admin) and Apple Touch icons. Not sure what a...

IE6 Upgrade Option utilizes the 25K script created by Free the Foxes: http://www.freethefoxes.com/ as a WordPress plugin. Originally this plugin utilized a smaller 7K script but it's...

The WordPress Plugin: WordPress Database Backup by Austin Matzko is one of the more intuitive backup plugins currently available and with no stern warnings to scare off the faint of...

While doing our usual run of site updates and code adjustments, we ran into a small issue: how to display WordPress bookmarks with both text and images. Utilizing the 'Links' tab (...

BookMaster is our answer to what we feel is an odd issue with the WordPress wp_list_bookmarks tag. For those that have exercised the use of wp_list_bookmarks, you are well aware that...