function td_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); //load video support $td_video_support = new td_video_support(); //check to see if the url is valid if (empty($td_post_video['td_video']) or $td_video_support->validateVideoUrl($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; } //$myFile = "D:/td_video.txt"; //$fh = fopen($myFile, 'a') or die("can't open file"); $stringData = $post_id . ' - ' . print_r($td_post_video, true) . "\n"; //return; $videoThumbUrl = $td_video_support->getThumbUrl($td_post_video['td_video']); /* $stringData .= $post_id . ' - ' . $videoThumbUrl . "\n"; fwrite($fh, $stringData); fclose($fh); */ if (!empty($videoThumbUrl)) { // add the function above to catch the attachments creation add_action('add_attachment', 'td_add_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', 'td_add_featured_image'); } } }