Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter tlock878

    (@tlock878)

    I’m not a developer but I asked good ol’ ChatGPT how to do it – not sure if this is helpful:

    Step 1: Register a Facebook App

    1. Go to the Facebook Developer site.
    2. Create a new app and get the App ID and App Secret.
    3. Generate a long-lived access token with the user_videos permission.

    Step 2: Fetch Facebook Video Thumbnail

    Use the Graph API to fetch the video thumbnail. Here’s an example function in PHP:

    function get_facebook_video_thumbnail($video_url) {
        // Parse the video ID from the URL
        preg_match('/facebook.com\/.*\/videos\/([0-9]+)/', $video_url, $matches);
        if (isset($matches[1])) {
            $video_id = $matches[1];
        } else {
            return false;
        }
    
        // Make an API call to get the video thumbnail
        $access_token = 'YOUR_ACCESS_TOKEN'; // Replace with your long-lived access token
        $api_url = "https://graph.facebook.com/v12.0/{$video_id}?fields=thumbnails&access_token={$access_token}";
        
        $response = wp_remote_get($api_url);
        if (is_wp_error($response)) {
            return false;
        }
    
        $body = wp_remote_retrieve_body($response);
        $data = json_decode($body, true);
    
        if (isset($data['thumbnails']['data'][0]['uri'])) {
            return $data['thumbnails']['data'][0]['uri'];
        }
    
        return false;
    }
    

    Step 3: Set the Featured Image in WordPress

    Use WordPress functions to set the featured image:

    function set_featured_image_from_facebook_video($post_id) {
        $post = get_post($post_id);
        if (has_post_thumbnail($post_id)) {
            return; // Exit if the post already has a featured image
        }
    
        // Extract the first Facebook video URL from the post content
        preg_match('/https:\/\/www.facebook.com\/.*\/videos\/[0-9]+/', $post->post_content, $matches);
        if (isset($matches[0])) {
            $video_url = $matches[0];
        } else {
            return;
        }
    
        // Get the thumbnail URL
        $thumbnail_url = get_facebook_video_thumbnail($video_url);
        if (!$thumbnail_url) {
            return;
        }
    
        // Upload the image to the media library and set it as the featured image
        $image_id = media_sideload_image($thumbnail_url, $post_id, null, 'id');
        if (is_wp_error($image_id)) {
            return;
        }
    
        set_post_thumbnail($post_id, $image_id);
    }
    
    // Hook into the post save action
    add_action('save_post', 'set_featured_image_from_facebook_video');
    
    Thread Starter tlock878

    (@tlock878)

    Hi Michael,

    Thank you for the quick response. Here is an example where a FB video is embedded:
    https://telemundominnesota.com/medidas-que-podemos-tomar-para-mejorar-el-puntaje-de-credito/

    Thread Starter tlock878

    (@tlock878)

    Hi Thomas,

    That did the trick.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)