Add checkbox to Featured Image metabox

This adds a checkbox to the Featured Image metabox in the Classic editor.

/**
 * Featured Image Display
 *
 * Adds checkbox to Featured Image metabox to automatically add it to post header
 *
 * @package      CoreFunctionality
 * @author       Bill Erickson
 * @since        1.0.0
 * @license      GPL-2.0+
**/
function ea_featured_image_display_settings( $content, $post_id ) {
	if( 'post' != get_post_type( $post_id ) )
		return $content;
	$field_id    = 'ea_featured_image_display';
	$field_value = esc_attr( get_post_meta( $post_id, $field_id, true ) );
	$field_text  = esc_html__( 'Display featured image at top of post.', 'ea' );
	$field_state = checked( $field_value, 1, false);
	$field_label = sprintf(
	    '<p><label for="%1$s"><input type="checkbox" name="%1$s" id="%1$s" value="%2$s" %3$s> %4$s</label></p>',
	    $field_id, $field_value, $field_state, $field_text
	);
	return $content .= $field_label;
}
add_filter( 'admin_post_thumbnail_html', 'ea_featured_image_display_settings', 10, 2 );

function ea_save_featured_image_display_settings( $post_ID, $post, $update ) {
	$field_id    = 'ea_featured_image_display';
	$field_value = isset( $_REQUEST[ $field_id ] ) ? 1 : 0;
	update_post_meta( $post_ID, $field_id, $field_value );
}
add_action( 'save_post', 'ea_save_featured_image_display_settings', 10, 3 );

You can then use the ea_featured_image_display meta value to determine if the featured image should be shown. Ex:

/**
 * Featured Image
 *
 */
function ea_single_featured_image() {
	$show = get_post_meta( get_the_ID(), 'ea_featured_image_display', true );
	if( is_single() && has_post_thumbnail() && $show )
		the_post_thumbnail( 'large' );
}
add_action( 'genesis_entry_header', 'ea_single_featured_image', 11 );

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk