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

How to use post_thumbnail_url filter in WordPress

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

post_thumbnail_url filter

Filters the post thumbnail URL.

apply_filters( 'post_thumbnail_url', string|false $thumbnail_url, int|WP_Post|null $post, string|int[] $size )

Description

This is filter hook , its filter the post thumbnail URL.
Its consists of three parameters, one is $thumbnail_url, second is Post Id or Post object and third for $size .

It is used in get_the_post_thumbnail() which returns post thumbnail URL.

Parameters

  • $thumbnail_url : (string|false) Post thumbnail URL or false if the post does not exist.
  • $post : (int|WP_Post|null) Post ID or WP_Post object. Default is global $post.
  • $size : (string|int[]) Registered image size to retrieve the source for or a flat array of height and width dimensions. Default ‘post-thumbnail’.

Live Example

To run the hook, copy the example below.

apply_filters( 'post_thumbnail_url', string|false $thumbnail_url, int|WP_Post|null $post, string|int[] $size )

The following example is for adding a hook callback.

// define the post_thumbnail_url callback 
function filter_post_thumbnail_url( $thumbnail_url , $post, $size) { 
    // make filter magic happen here... 
    return $thumbnail_url; 
}; 
         
// add the filter 
add_filter( 'post_thumbnail_url', 'filter_post_thumbnail_url', 10, 1 ); 

To remove a hook callback, use the example below.

// remove the filter 
remove_filter( 'post_thumbnail_url', 'filter_post_thumbnail_url', 10, 1 ); 

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.