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.
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?
Filters the post thumbnail URL.
apply_filters( 'post_thumbnail_url', string|false $thumbnail_url, int|WP_Post|null $post, string|int[] $size )
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.
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 )
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 );
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 );
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 );
If you need customization or have any questions, feel free to contact us.
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.