My site Beta Fox uses Yoast SEO plugin to help deal with SEO common practices, however an issue has arisen. In beginning of last year WordPress didn't fully support webp image types, neither Yoast SEO. Things changed near the end of year, but Yoast SEO unbelievably refuses to support webp (the types Google is forcing upon on us to use). The problem is the fact there's no open graph for image attachments in any page of the site, so that means link preview doesn't show any image.

add_action('wp_head', 'add_link_prev_img');
    function add_link_prev_img () {
        $attachment_id = get_post_thumbnail_id( $post_id ); $image_attributes = wp_get_attachment_image_src( $attachment_id, "full" );
        $fotoo = $image_attributes[0];
        if (!empty($fotoo) && get_post_mime_type( get_post_thumbnail_id()) == 'image/webp') {
        <meta property="og:image" content="<?php echo $fotoo;?>"> 

The code above was supposed to add the meta tag that is missing in pages/posts of my site, but doesn't work as intended. It causes critical error in the site. Every page needs to show featured image or something similar if available. Please, help!

  • 2
    Well, regarding the critical error, did you actually add the HTML just like that? It should be ?><meta ...><?php .. Also, 1) $post_id is never defined in your code, and 2) wp_get_attachment_image_src() may return false, so you can't simply do $image_attributes[0] or assume the $image_attributes is an array. You should also escape the $fotoo when echoing its value.
    – Sally CJ
    May 28, 2022 at 10:32


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.