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

How to use post_thumbnail_size filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
July 25, 2022
5 minutes read

post_thumbnail_size filter

Filters the post thumbnail size.

apply_filters('post_thumbnail_size', string|int[] $size, int $post_id)

Description

This is a filter hook that filters the post thumbnail size. It consists of two parameters: $size and Post Id.

It is used in get_the_post_thumbnail() to get the post thumbnail.

Parameters

  • $size: (string|int[]) Requested image size. Can be any registered image size name, or an array of width and height values in pixels (in that order).
  • $post_id: (int) The post ID.

Live Examples

Basic Usage

To run the hook, copy the example below.

  $size = apply_filters('post_thumbnail_size', $size, $post_id);
  
  if (!empty($size)) {
      // everything has led up to this point...
  }
  

Adding a Hook Callback

The following example is for adding a hook callback.

  // define the post_thumbnail_size callback 
  function weplugins_filter_post_thumbnail_size($size, $post_id) {
      // make filter magic happen here...
      return $size;
  }

  // add the filter 
  add_filter('post_thumbnail_size', 'weplugins_filter_post_thumbnail_size', 10, 2);
  

Customizing Thumbnail Size

In this example, we change the thumbnail size to ‘medium’ for a specific post ID.

  function weplugins_custom_post_thumbnail_size($size, $post_id) {
      if ($post_id == 42) {
          return 'medium';
      }
      return $size;
  }

  add_filter('post_thumbnail_size', 'weplugins_custom_post_thumbnail_size', 10, 2);
  

Access Premium WordPress Plugins

Contact Us

If you need customization, contact us.

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.