0

I am trying to automatically add a video thumbnail from Vimeo or YouTube as the featured image when a new custom post type video is add/updated. The video ID is an advanced custom field. Here's my non-working code:

/**
 * Gets a vimeo thumbnail url
 * @param mixed $id A vimeo id (ie. 1185346)
 * @return thumbnail's url
*/
function getVimeoThumb($id) {
    $data = file_get_contents("http://vimeo.com/api/v2/video/".$id.".json");
    $data = json_decode($data);
    return $data[0]->thumbnail_large;
}

/**
 * Gets a youtube thumbnail url
 * @param mixed $id A youtube id
 * @return thumbnail's url
*/
function getYoutubeThumb($id) {
    $data = file_get_contents("https://www.googleapis.com/youtube/v3/videos?key=AIzaSy...&part=snippet&id=".$id);
    $data = json_decode($data);
    return $json->items[0]->snippet->thumbnails->medium->url;
}

// Add featured image for videos
function addVideoThumb($post_ID) {

    if(!has_post_thumbnail($post_ID)):

        if(get_field("source", $post_ID) == "youtube"):
            $videoID = get_field("youtube_video_id", $post_ID);
            $imageURL = getYoutubeThumb($videoID);          
        else:
            $videoID = get_field("vimeo_video_id", $post_ID);
            $imageURL = getVimeoThumb($videoID);
        endif;
    
        //echo $imageURL;

        // download image from url
        $tmp = download_url($imageURL);

        $file = array(
            'name' => basename($imageURL),
            'tmp_name' => $tmp
        );

        // create attachment from the uploaded image
        $attachment_id = media_handle_sideload($file, $post_ID);

        // set the featured image
        update_post_meta($post_ID, '_thumbnail_id', $attachment_id);

    endif;
}
add_action('save_post_interel-tv-videos', 'addVideoThumb', 10, 3);

The function doesn't seem to be triggered at all as I tried to add a JavaScript alert to it and nothing pops up. Any help? Thanks in advance!

6
  • Check out this answer by Tom Nowell. Function media_sideload_image allows you to pass a URL to an image and uploads it to a post_id. That answer uses some hooks to assign that new image as a featured image which uses _thumbnail_id. by default.
    – Howdy_McGee
    Apr 5, 2016 at 14:24
  • Also doesn't work. Think the problem is the function not being triggered at all. But no idea why...
    – rjpedrosa
    Apr 5, 2016 at 14:51
  • Where are you putting these functions and which functions aren't firing?
    – Howdy_McGee
    Apr 5, 2016 at 14:52
  • On functions.php The function addVideoThumb assigned to the add_action() doesn't seem to be triggered when I update a video.
    – rjpedrosa
    Apr 5, 2016 at 15:07
  • Found a couple of issues with the code. Will fix them and get back here.
    – rjpedrosa
    Apr 5, 2016 at 15:12

1 Answer 1

0

Finally sorted it out. As I am using Advanced Custom Fields, I need to use the acf/save_post hook with a priority greater than 10, so the get_field() functions are available.

Downside is that this hook runs for every post type. As opposed to the WP native save_post hook, with the ACF one you can't hook it for a specific custom post type.

So I also added a condition to check if the post is of type "interel-tv-videos", like if(!has_post_thumbnail($post_ID) and get_post_type($post_ID) == "interel-tv-videos"):

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.