0

I have created a wordpress plugin that fetches data from a API and creates custom posts. Also i have set featured image to post from data taken from the API. My problem is some posts show the url preview with image correctly but some posts do not the show the url preview with image.

For example :

https://expertrepublic.com/curveup shows the url preview correctly when shared on facebook. But https://expertrepublic.com/santhosh-rupert does not show the url preview correctly. The featured image is set to have a size of 300*300.

This is the code i have used to set the featured image

// Add Featured Image to Post
            $url              = $this->getImageUrl($expert_profilepic);
            $image_url        = $url; // Define the image URL here
            $image_name_main  = substr("$expert_profilepic", strrpos("$expert_profilepic", '/') + 1);
            $image_name       = 'profileImage.png';
            $upload_dir       = wp_upload_dir(); // Set upload folder
            $image_data       = file_get_contents($image_url); // Get image data
            $unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
            $filename         = basename( $unique_file_name ); // Create image file name
            
            // Check folder permission and define file location
            if( wp_mkdir_p( $upload_dir['path'] ) ) {
                $file = $upload_dir['path'] . '/' . $filename;
            } else {
                $file = $upload_dir['basedir'] . '/' . $filename;
            }

            // Create the image  file on the server
            file_put_contents( $file, $image_data );

            // Check image file type
            $wp_filetype = wp_check_filetype( $filename, null );

            // Set attachment data
            $attachment = array(
                'post_mime_type' => $wp_filetype['type'],
            );

            // Create the attachment
            $attach_id = wp_insert_attachment( $attachment, $file, $inserted_expert );

            // Include image.php
            require_once(ABSPATH . 'wp-admin/includes/image.php');

            // Define attachment metadata
            $attach_data = wp_generate_attachment_metadata( $attach_id, $file );

            // Assign metadata to attachment
            wp_update_attachment_metadata( $attach_id, $attach_data );

            // And finally assign featured image to post
            set_post_thumbnail( $inserted_expert, $attach_id );

This issue comes only when sharing on facebook. Other platforms shows the url preview correctly. Ex: Twitter,slack,whatsapp etc..

CraZyDroiD
  • 6,622
  • 30
  • 95
  • 182
  • Are you adding open graph meta data or just standard meta data? This is a relatively common problem with Facebook.The reason is usually to do with caching (either in a plugin, on the server or in Facebook itself). – FluffyKitten Jul 12 '20 at 06:41
  • Agree with @FluffyKitten. Try to add a query at the end of the post missing the thumb ( like https://expertrepublic.com/santhosh-rupert?v=new ). – Raffobaffo Jul 12 '20 at 13:36

0 Answers0