A picture is worth a thousand words – unless it’s been resized inappropriately, in which case it’s worth negative one-hundred words. Okay – so maybe that’s a lesser known version of an old saying or something I just made up, but the point is: WordPress is very powerful when it comes to thumbnails and image sizes – you just need to know how to deal with them. Read on for everything you need to know about image sizes in WordPress and managing featured images.
Basics First
The medium and large setting work slightly differently, in that you’ll specify maximum dimensions for both width and height, and the images will be scaled down accordingly. If the image is too small, those image sizes simply wont be created. When you upload a new image, the original is saved and available to insert into a post at full size, and the other registered image sizes are automatically created.
These three image sizes, as well as the original full size, are the only ones available when you edit a post and insert media, so I tend to set large as the absolute width of my theme for full width pages and posts, and medium as width of the typical content column.
Making More Sizes
add_image_size( 'my-thumbnail', 400, 200, true);
Each new image size needs a name, width and height dimensions, and whether or not images should cropped to exactly this size (true or false). For structural parts of a theme or widget, you generally would want to crop so it doesn’t break the layout.Featured Image
add_action('save_post', 'wpds_check_thumbnail');
add_action('admin_notices', 'wpds_thumbnail_error');
function wpds_check_thumbnail($post_id) {
// change to any custom post type
if(get_post_type($post_id) != 'post')
return;
if ( !has_post_thumbnail( $post_id ) ) {
// set a transient to show the users an admin message
set_transient( "has_post_thumbnail", "no" );
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'wpds_check_thumbnail');
// update the post set it to draft
wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
add_action('save_post', 'wpds_check_thumbnail');
} else {
delete_transient( "has_post_thumbnail" );
}
}
function wpds_thumbnail_error()
{
// check if the transient is set, and display the error message
if ( get_transient( "has_post_thumbnail" ) == "no" ) {
echo "<div id='message' class='error'><p><strong>You must select Featured Image. Your Post is saved but it can not be published.</strong></p></div>";
delete_transient( "has_post_thumbnail" );
}
}
WordPress provides a convenient function for getting the featured image and using it in themes:the_post_thumbnail('my-thumbnail',array('class'=>'my_post_thumbnail_css_class'));The function takes 2 parameters: the named size you’re looking for, and any attributes you want to pass in, like a custom CSS class (note: you can’t override the alt attribute). If you’re modifying a really old theme, you may also need to add the following to your functions.php:add_theme_support('post-thumbnails');
If you’d rather just get the actual URL of the featured image rather than output the required HTML too, try this instead (getting the medium image size in this example):$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'medium');
echo $thumbnail[0];
Regenerating Images
Secondly, even if you’re no longer using a certain size, they will remain on the server – WordPress won’t delete unused images for you. On a site like MakeUseOf with hundreds of thousands of images, this means a couple of gigabytes wasted. For smaller sites, the Image Cleanup plugin will help by scanning and giving you the option to delete; but larger sites will need to learn some command-line and regex fu (see our quick guide to getting starting with the Linux command line). Always take a full backup first, just in case it deletes something it shouldn’t have.
My Theme Doesn’t Change
WordPress is nearly 11 years old, so it’s a testament to its power and flexibility that it’s one of the few web apps that has been kept alive for so long and not replaced. Features like post thumbnails are now ubiquitous on the web, and to its credit, WordPress has always kept up with design trends. Mind you, Ghost is looking good.
Having trouble with featured images? Post in the comments and I’ll see if I can help you out.

No comments:
Post a Comment