I’ve built a couple themes where I’ve needed to give users more control over how the featured image is displayed for individual posts. The obvious place to put these options are in the thumbnail metabox itself.
Display Thumbnail Landscape
In this theme the default display of the thumbnails is a cropped to a square next to the title. But if the user had a large landscape image, they could choose to display it full width instead.
Option to Display Featured Image in Post Automatically
In this theme the featured image is used as a thumbnail on portfolio archive pages, but it is also displayed full size on the portfolio post types. This option gives users the option to use a different image in the actual post if they prefer.
Add the Option
Adding the option is fairly simple, you’ll just need to hook into the admin_post_thumbnail_html filter:
/**
* Adds a checkbox to the featured image metabox.
*
* @param string $content
*/
function prefix_featured_image_meta( $content ) {
global $post;
$text = __( 'Don\'t display image in post.', 'prefix' );
$id = 'hide_featured_image';
$value = esc_attr( get_post_meta( $post->ID, $id, true ) );
$label = '<label for="' . $id . '" class="selectit"><input name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $value . ' "'. checked( $value, 1, false) .'> ' . $text .'</label>';
return $content .= $label;
}
add_filter( 'admin_post_thumbnail_html', 'prefix_featured_image_meta' );
Sanitize and Save the Option
This option will need to be sanitized and saved when the post is updated- like any other custom field in a metabox. Here’s how that could be done:
/**
* Save featured image meta data when saved
*
* @param int $post_id The ID of the post.
* @param post $post the post.
*/
function prefix_save_featured_image_meta( $post_id, $post, $update ) {
$value = 0;
if ( isset( $_REQUEST['hide_featured_image'] ) ) {
$value = 1;
}
// Set meta value to either 1 or 0
update_post_meta( $post_id, 'hide_featured_image', $value );
}
add_action( 'save_post', 'prefix_save_featured_image_meta', 10, 3 );
Credits
The thumbnail images above are from Alexander S. Kunz Photography.