/** * Downloads the video thumb on the save_post hook * @param $post_id */ static function on_save_post_get_video_thumb($post_id) { //verify post is not a revision if (!wp_is_post_revision($post_id)) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } $td_post_video = get_post_meta($post_id, 'td_post_video', true); //check to see if the url is valid if (empty($td_post_video['td_video']) or self::validate_video_url($td_post_video['td_video']) === false) { return; } if (!empty($td_post_video['td_last_video']) and $td_post_video['td_last_video'] == $td_post_video['td_video']) { //we did not update the url return; } $videoThumbUrl = self::get_thumb_url($td_post_video['td_video']); if (!empty($videoThumbUrl)) { self::$on_save_post_post_id = $post_id; // add the function above to catch the attachments creation add_action('add_attachment', array(__CLASS__, 'on_add_attachment_set_featured_image')); // load the attachment from the URL media_sideload_image($videoThumbUrl, $post_id, $post_id); // we have the Image now, and the function above will have fired too setting the thumbnail ID in the process, so lets remove the hook so we don't cause any more trouble remove_action('add_attachment', array(__CLASS__, 'on_add_attachment_set_featured_image')); } } }