phpwordpressoembedyoast

How can I customize my WordPress post's generated wp-json oembed?


I've noticed through LinkedIn Inspector that my post's thumbnail is being grabbed from the oembed generated by wp-json rather than my Yoast SEO open graph meta tags. How can I customized the oembed to not use the featured image and use a custom image? I've already set a custom image through Yoast SEO for Facebook and Twitter and I assumed LinkedIn would pick up the OG meta tags from the facebook side of things but it does not do that, it skips it in favor of the oembed generated XML file in the post.


Solution

  • You can adjust images using the Schema API in Yoast SEO v14+.

    <?php
    
     /**
     * Changes url of Main_Image (ImageObject) Schema data.
     *
     * @param array $data Schema.org Main_image data array.
     * @return array Schema.org Main_image data array.
     *
     * @link https://developer.yoast.com/features/schema/api#to-add-images-to-your-schema
     */
    add_filter( 'wpseo_schema_main_image', 'example_change_wpseo_image' );
    function example_change_wpseo_image( $data ) {
    
        $attachment_ID = '123';
        $img_src = wp_get_attachment_image_src( $attachment_ID, 'full' );
    
        if (! empty( $img_src )){
          $url = $img_src[0];
          $data['url'] = $url;
        }
    
        return $data;
    }