Custom Field in Featured image for A particular post

Note that there are additional input arguments available for the admin_post_thumbnail_html filter callback, namely $post->ID and $thumbnail_id:

/**
 * Filters the admin post thumbnail HTML markup to return.
 *
 * @since 2.9.0
 * @since 3.5.0 Added the `$post_id` parameter.
 * @since 4.6.0 Added the `$thumbnail_id` parameter.
 *
 * @param string $content      Admin post thumbnail HTML markup.
 * @param int    $post_id      Post ID.
 * @param int    $thumbnail_id Thumbnail ID.
 */
 return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id );

that’s applied within the _wp_post_thumbnail_html() function.

Here’s an example how you can avoid the global post in your snippet, by using the $post->ID input argument instead:

function wpse250432_add_featured_description( $content, $post_id, $thumbnail_id ) {

    $small_description = get_post_meta( $post_id, 'thumbnail_description', true );

    // Target only the 'post' post type
    if ( 'post' === get_post_type( $post_id ) ) 
      $content .= '<div id="thumb_desc_container">...</div>'; // Edit

    return $content;
}
add_filter( 'admin_post_thumbnail_html', 'wpse250432_add_featured_description', 10, 3 );

If you really need to restrict this filtering to the post.php and post-new.php screens and the get-post-thumbnail-html ajax call, then you could add a check like:

if( 
       ! did_action( 'load-post.php' ) 
    && ! did_action( 'load-post-new.php' ) 
    && ! did_action( 'wp_ajax_get-post-thumbnail-html' )
)
    return $content;

but then I assume you’re e.g. calling the “private” _wp_post_thumbnail_html() core function elsewhere? But these (underscored) private functions are not intended for use by plugin and theme developers, but only by other core functions.

Leave a Comment