feb 17 2012
Making WordPress image management easier for your editors
I am managing a WordPress site with a lot of editors and contributors. They are not all equally up to pace with WordPress and how it works, or at least they have different ways of doing things. One thing that has proved to be a challenge is the way WordPress handles thumbnails. My editors and contributors often miss the ‘set as post thumbnail’ link, leading to the lack of thumbnails on the front page.
In this post I will try to explain, how I have made a fallback, making it sort of OK for my users to forget about the thumbnail.
A little history
Once upon a time, before version 2.9, WordPress did not support post thumbnails. But there were work arounds. One of the best, I ever found, was from a website called wp-fun.co.uk. Sadly, the site is gone today, but I managed to dig up the old post ‘Easy Peasy Images Suggestion Roundup’ by Andrew Rickmann on Archive.org.
Andrew wrote a nice little PHP class to include in your functions file, which gets the first thumbnail image attachment from WordPress posts. Luckily, I also managed to dig that one up on Archive.org. Go grab it here: post_imagephp.txt (remember to rename .txt to .php, before including it in your functions.php file).
“So why are You telling me about antiques like this?”, You ask. Because we can use it with advantage in our fallback for people, who forget to click the little ‘set as post thumbnail’ link.
That way, a thumbnail will still be displayed with the post, as long as the editor has just attached an image. If there are no images attached at all, we can always display a default thumbnail.
The fallback
Let’s take a peek at the code. Put the following where you would normally use the_post_thumbnail().
<?php
// First of all, check if there are any image attachments at all.
$imgargs = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => -1,
);
$attachments = get_posts($imgargs);
// Next, check if there is a manually chosen thumbnail.
if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
}
// If there are no manually chosen thumbnails, check if there is an image attachment.
elseif ( $attachments ) {
// If there is an image attachment, call Andrew Rickmanns the_image function to get the thumbnail
the_image('thumbnail','attachment-thumbnail wp-post-image',false,false,true);
} else {
// If there are no attachments at all, display a default thumbnail.
?><img class="attachment-thumbnail wp-post-image" alt="" src="<?php bloginfo('stylesheet_directory'); ?>/the/path/to/your/image.jpg" />
<?php }?>
Requirements:
Remember to include this file, renamed to .php, in your functions.php: post_imagephp.txt


