5

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?

5
+50

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.

  • Thanks it works! But when filter is add_filter( 'admin_post_thumbnail_html', 'add_featured_description', 10, 3 ); the function 'add_featured_description' ->function add_featured_description( $content, $post_id, $thumbnail_id ) shows error, when the function contains two parameters as function add_featured_description( $content, $post_id ) it works fine. It will be great if you add the meaning of the parameters 10 and 3. Thank you so much anyway :) – Prifulnath Dec 30 '16 at 3:25
  • 1
    Glad to hear it worked for you. The 10 here is the (default) filter priority and 3 is the number of input arguments for the filter callback. If the number of input arguments is 1 then we can only access $content within our filter callback. If it's 2 then we get access to both $content and $post_id. Note that I prefixed the filter callback, to avoid possible name collisions with such a general function name as add_featured_description(). PHP namespaces is a good alternative. @Kuttoozz – birgire Dec 30 '16 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.