Custom Field in Featured image for A particular post

I have added a custom textarea to my featured image only to post, Using the following code.

function add_featured_description( $content ) {
    global $post;
    $small_description = get_post_meta( $post->ID,'thumbnail_description', true ) !== '0' ? get_post_meta( $post->ID,'thumbnail_description', true ) : '';
    global $pagenow;
    if (is_admin() && ($pagenow == 'post-new.php' || $pagenow == 'post.php') && ( get_post_type()=='post' ) ) {
      return $content .= '<div id="thumb_desc_container">
                            <textarea name="thumbnail_description" id="thumbnail_description" rows="4" style="width:100%;">'.$small_description.'</textarea>
                            <p class="hide-if-no-js howto" id="set-post-thumbnail-desc">Enter the image description.</p>
                          </div>';
    } else{
      return $content;
    }
}
add_filter( 'admin_post_thumbnail_html', 'add_featured_description');

It works fine!!! This code shows the extra textarea only in POST. But when i selected a featured image else part is executed(only featured image is returned).
When adding an image $pagenow contains admin-ajax.php and get_post_type() is null. global variables $post produces a null value. What should i do now?

1
1

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