Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Save Featured Image If It's a youtube video
- add_action('save_post', 'wds_video_sideload_post_thumb');
- function wds_video_sideload_post_thumb() {
- global $post;
- $youtube_url = get_post_meta( $post->ID, 'videobox', true );
- $youtubeid = youtubeid($youtube_url);
- $thumb_url = 'http://img.youtube.com/vi/'. $youtubeid .'/0.jpg';
- require_once(ABSPATH . 'wp-admin/includes/file.php');
- require_once(ABSPATH . 'wp-admin/includes/media.php');
- set_time_limit(300);
- if ( ! empty($thumb_url) ) {
- // Download file to temp location
- $tmp = download_url( $thumb_url );
- // Set variables for storage
- // fix file filename for query strings
- preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $thumb_url, $matches);
- $file_array['name'] = basename($matches[0]);
- $file_array['tmp_name'] = $tmp;
- // If error storing temporarily, unlink
- if ( is_wp_error( $tmp ) ) {
- @unlink($file_array['tmp_name']);
- $file_array['tmp_name'] = '';
- }
- // do the validation and storage stuff
- $thumbid = media_handle_sideload( $file_array, $post->ID, $desc );
- // If error storing permanently, unlink
- if ( is_wp_error($thumbid) ) {
- @unlink($file_array['tmp_name']);
- return $thumbid;
- }
- }
- set_post_thumbnail( $post, $thumbid );
- }
- function youtubeid($url) {
- if (preg_match('%youtube\\.com/(.+)%', $url, $match)) {
- $match = $match[1];
- $replace = array("watch?v=", "v/", "vi/");
- $match = str_replace($replace, "", $match);
- $match = substr( $match, 0, ( strpos( $match, '&') ) );
- }
- return $match;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement