08.14
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:
1 | <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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php /* Plugin Name: Post thumbnail support Version: 1.0 Plugin URI: Description: This plugin enables post thumbnail support Author: DjZoNe Author URI: http://djzone.im */ add_action( 'init', 'my_theme_init' ); function 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | add_filter('update_post_metadata', 'my_update_post_metadata',10,5); function my_update_post_metadata($obsolete,$post_id,$meta_key,$meta_value,$prev_value) { if($meta_key == '_thumbnail_id') { delete_post_meta($post_id, "image"); 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[0]) $image = str_replace(home_url('/'),'',$thumb_image[0]); if(!empty($image)) { add_post_meta($post_id, "image", $image); } } } } |
After activating my plugin with those two functions in it it solved my thumbnail issues.
No Comment.
Add Your Comment