• Resolved tlock878

    (@tlock878)


    I love this plugin but was wondering if you could make it work with Facebook embeds as well instead of just YouTube and Vimeo?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Michael Beckwith

    (@tw2113)

    The BenchPresser

    If they offer a way to fetch and import a screenshot of the video into the media library, we can explore the idea. However, at least with the various link examples I’ve tried, none of them are even working with embedding into a WordPress block.

    Sorry, this content could not be embedded.

    If you have an example URL that we could try with, we can try once more with at least the embedding part.

    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/

    Plugin Support Michael Beckwith

    (@tw2113)

    The BenchPresser

    Assuming you’re using this url for the actual embed:

    https://www.facebook.com/TelemundoMSP/videos/372942438505669/

    then I’m still getting the same embed error message. You could also potentially be using a different plugin to enable Facebook embeds like this, but that adds an extra dependency to us, just to get started. There’s still the static screenshot availability to resolve.

    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');
    
    Plugin Support Michael Beckwith

    (@tw2113)

    The BenchPresser

    For what it’s worth, I wasn’t worried about the coding aspect, we’d be able to figure out that part with enough time. My biggest questions/concerns were how much effort would be needed, and if thumbnails for a given video were available in general.

    Based on your line here:

    $api_url = "https://graph.facebook.com/v12.0/{$video_id}?fields=thumbnails&access_token={$access_token}";

    It looks like some are/will be. However there’s the new caveat of needing to do authentication/authorization flows to achieve, instead of having some public URL endpoints like Youtube and Vimeo provide.

    Not completely sure how far down that proverbial rabbit hole I want to have the plugin go.

    That said, I checked our github repo and found this slightly dated but very likely still relevant request for the same Facebook topic. I have cross posted about this forum thread to that, for some re-enforcement.

    https://github.com/WebDevStudios/Automatic-Featured-Images-from-Videos/issues/64

    Crossposting the below from the issue, for some easier visibility:

    I think the biggest things are going to be:

    1. Setting up and wiring up an authentication method to get the necessary access tokens, as well as keep those refreshed and renewed.
    2. Making sure we keep up with Facebook’s ongoing changes with their graph platform.

    One nice thing that’s been evident for a long time with Vimeo and Youtube is that those endpoints have simply not changed at all, allowing for a lot of stability with the plugin.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.