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
January 16, 2023
5 minutes read

Welcome to this detailed page about the post_thumbnail_url filter in WordPress! This filter allows you to modify the post thumbnail URL dynamically. It’s like having the power to change how thumbnails appear on your site with just a few lines of code. Pretty cool, right?

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 a filter hook that filters the post thumbnail URL. It consists of three parameters: $thumbnail_url, Post ID or Post object, and $size.

It is used in get_the_post_thumbnail() which returns the 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 )

Example 1: Adding a Hook Callback

This example demonstrates how to add a filter callback for the post_thumbnail_url filter.

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

Example 2: Modifying the Thumbnail URL

In this example, we modify the post thumbnail URL to a custom URL if certain conditions are met.

	function weplugins_custom_thumbnail_url( $thumbnail_url , $post, $size) { 
	    if ( $post->ID == 42 ) { 
	        return 'https://example.com/custom-thumbnail.jpg'; 
	    } 
	    return $thumbnail_url; 
	}; 
	         
	add_filter( 'post_thumbnail_url', 'weplugins_custom_thumbnail_url', 10, 3 ); 
	

Example 3: Removing a Hook Callback

To remove a hook callback for the post_thumbnail_url filter, use the example below.

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

Access Premium WordPress Plugins

Contact Us

If you need customization or have any questions, feel free to 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.