Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use admin_post_thumbnail_html filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 12, 2023
5 minutes read

 

admin_post_thumbnail_html filter

This hook is quite handy, especially when you need to tweak the admin post thumbnail HTML in WordPress. To use the admin_post_thumbnail_html filter, you first need to register it using add_filter. You can place this code in the functions.php file of your active theme or within a custom WordPress Plugin. It’s always a good idea to go with a custom plugin so that your changes remain intact even when your theme updates.

Let’s dive into some live examples to see how this hook works.

Example 1: Adding Custom Text to Featured Image

In this example, we modify the featured image admin text to include a note about the recommended image size.

    function weplugins_filter_featured_image_admin_text( $content, $post_id, $thumbnail_id ) {
        $help_text = '' . __( 'Please use an image that is 1170 pixels wide x 658 pixels tall.', 'my_domain' ) . '';
        return $help_text . $content;
    }
    add_filter( 'admin_post_thumbnail_html', 'weplugins_filter_featured_image_admin_text', 10, 3 );
    

Example 2: Modifying Default Admin Post Thumbnail HTML

Here’s how you can modify the default admin post thumbnail HTML according to your requirements.

    function weplugins_modify_admin_post_thumbnail_html_defaults( $content, $post_id, $thumbnail_id ) { 
        // Update the $content variable according to your website requirements and return this variable.
        return $content; 
    }
    // Add the filter
    add_filter( "admin_post_thumbnail_html", "weplugins_modify_admin_post_thumbnail_html_defaults", 10, 3 );
    

Example 3: Removing a Hook Callback

If you ever need to remove a registered hook callback, see the example below.

    remove_filter( "admin_post_thumbnail_html", "weplugins_modify_admin_post_thumbnail_html_defaults", 10, 3 );
    

Ensure you provide the same callback function name, priority, and number of arguments when removing the hook callback.

Parameters

Here are the parameters required to use this hook:

  • $content: (string) Admin post thumbnail HTML markup.
  • $post_id: (int) Post ID.
  • $thumbnail_id: (int|null) Thumbnail attachment ID, or null if there isn’t one.

Access Premium WordPress Plugins

Contact Us

If you’re having any trouble using this hook or need customization, feel free to contact us. We’re here to help!

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.