Or almost always.
Have you ever experienced after a theme switch, or a core upgrade that things don’t work the way they did, and your mighty thumbnail system gone? About a week ago I’ve updated a client’s website, form a pretty old WordPress to the latest one. And this upgrade was not painless at all. Because a plugin or a function disappeared from the admin.
This tiny function was to add thumbnails to a post, not the official WordPress post-thumbnail way, but a sneaky, hacky way. Maybe that’s the reason it’s gone forever. However there was a theme that relies on this functionality what should a bug thinker coder like me do?
Well the problem in technical terms was simple.
The theme is looking for a post meta for each post named ‘image’, this should contain not the full URL, but the relative path to the image, from the root installation directory.
But what should I do? Should I rewrite the whole theme, to use the standard WordPress ‘_thumbnail_id’ meta_key method? Or should I figure out something else?
Finally I came up with a solution, that maintains the compatibility with the old method, but let the admin use the standardized WordPress thumbnail system. Let’s go.
First of all I let you know, that the theme was not developed by me, so I want to modify it as little as possible.
Secondly, this theme was as old as the WordPress installation (more than 3 years old), and it does not support new features, like nav-menus and post-thumbnails.
So, my method was:
1., I won’t rewrite the theme, which was full of this “beautiful” code lines:
<img src="<?php echo bloginfo('template_url'); ?>/timthumb.php?src=/<?php echo get_post_meta($post->ID, "image", true); ?>&w=630&h=250&zc=1&q=100" alt="<?php the_title(); ?>" />
These occured more than 40 times in 10 files, at least. I don’t even want to see them.
2., Add post-thumbnail support through a plugin
This was pretty easy:
Plugin Name: Post thumbnail support
Description: This plugin enables post thumbnail support
Author URI: http://djzone.im
add_action( 'init', 'my_theme_init' );
add_theme_support( 'post-thumbnails' );
3., Use the thumbnail system to update this field
First I thought that I should catch the get_post_meta calls, and look for _thumbnail_id instead, and if that exists we should give back that. But it’s a resource heavy thing, and because the hook i wanted to use for this trick will run before the wp_cache_get stuff the result can’t be cached, so this tiny little compatibility hack would require an extra 20-30 SQL select on it’s own at every page load.
So it’s much more clever to generate this extra field when the post thumbnail is updated.
if($meta_key == '_thumbnail_id')
if(!empty($meta_value) && is_numeric($meta_value))
$thumb_id = (int) $meta_value;
$thumb_image = wp_get_attachment_image_src( $thumb_id, 'full' );
if($thumb_image) $image = str_replace(home_url('/'),'',$thumb_image);
add_post_meta($post_id, "image", $image);
After activating my plugin with those two functions in it it solved my thumbnail issues.