• According to a condition, I need to change the featured image (I can’t do it manually because there are thousand of posts, my blog started in 2007).
    In other words, if the featured image of a post is a .jpg or .png or haven’t a featured image, I whant to replace/set it for an uploaded .webp that respects the google recomendations.
    Is it possible?
    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s likely possible. The ability to do so depends on how your theme outputs featured images. If it calls the_post_thumbnail(), you can use the “post_thumbnail_html” to alter the HTML that’s about to be output. For example, search for .jpg or .png file extensions and replace or append .webp to the URL.

    You can apply whatever logic is necessary in support, such as ensuring a .webp version of the image is available. Other image attributes may need alteration besides the src URL. All the HTML is there for you to alter as needed.

    This is only one possible solution. Others likely exist, each with their own benefits and problems. For example, “wp_get_attachment_image_src” filter could be used, but the filter fires for all images, featured or not. Could be a good or bad thing. But it would not affect other image attributes that might also need to be changed.

    Thread Starter roxer82

    (@roxer82)

    Hello @bcworkz, thanks for your answer.

    Yes, I’m using Astra and it uses the_post_thumbnail(), but I’m not only whant to change it there, I whant to change it for everything, so that, the SEO plugin would take it as the featured image too.

    In other words, if a post hasn’t a featured image or the featured image is not a .webp, I want everithing in WordPress to return a specific image.

    Moderator bcworkz

    (@bcworkz)

    “post_thumbnail_html” filter will let you alter the HTML output any way you like. It’s up to your added callback to apply your desired logic. I don’t see any reason your logic could not be applied by the callback. This of course will only affect the call to the_post_thumbnail(), not any SEO structured data. Unless your SEO plugin also gets the featured image from the same function. Unlikely I think.

    To apply the same logic to SEO structured data, it once again depends on how the plugin gets the featured image URL. There’s likely a different filter you could use. I recommend asking for advice through your SEO plugin’s dedicated support channel.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Change featured image from functions.php’ is closed to new replies.