Missing feature image link function

Yes, you’ve guessed it right. You will need to use Custom Fields in order to define the URL for the Image that you wish to use as Featured Image for the post.

Next part is to tell the WordPress to use the Image from URL you’ve in Custom Field as Featured Image. Here, you can use filter called “post_thumbnail_html”. The Filter is defined in “wp-includes/post-thumbnail-template.php” file under function “get_the_post_thumbnail” and also available in the latest version of WordPress.

The definition goes like this:

return apply_filters( 'post_thumbnail_html', $html, $post_id, $post_thumbnail_id, $size, $attr );

Here is a sample use of it:

function custom_post_thumbnail($html)
{ 
    return "<img src="https://www.google.co.in/images/srpr/logo3w.png" />";
}
add_filter('post_thumbnail_html', 'custom_post_thumbnail');

Note that above code snippet is just to give you an idea of how to use this filter. You will need to add required code to get the Custom Field value and return proper IMG tag. The best thing I would suggest to do is merely parse the Image SRC part of $html and replace it with your URL. Also don’t forget to check if the Custom Field is empty; in that case, just return the default $html without altering.

I hope this helps.

Cheers,

Ruturaaj.

Leave a Comment