Created
October 30, 2014 21:48
-
-
Save jamesmehorter/011cfeec683d42bf69ae to your computer and use it in GitHub Desktop.
Import external url/image as WordPress attachment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Import the YouTube video thumbnail as the post's featured image | |
// Only proceed if there is a youtube video thumbnail | |
/* | |
Example $youtube_video_data: | |
array( | |
'id' => 'gvk6TwmF0cw', | |
'title' => 'Tyler Posey talks to Variety about being honored at Variety Power of Youth', | |
'link' => 'http://www.youtube.com/watch?v=gvk6TwmF0cw', | |
'thumbnail' => 'http://i.ytimg.com/vi/gvk6TwmF0cw/hqdefault.jpg', | |
'desc' => 'Tyler Posey talks to Variety about being honored at Variety Power of Youth.', | |
'duration' => 33, | |
'published' => '2013-07-30T16:52:41.000Z', | |
'viewcount' => 1, | |
) | |
*/ | |
if ( isset( $youtube_video_data['thumbnail'] ) ) { | |
if ( ! empty( $youtube_video_data['thumbnail'] ) ) { | |
// Does the post already have a featured image? | |
if ( has_post_thumbnail( $post->ID ) ) { | |
// Yes, it does have a featured image, DO NOT bring the image from YouTube over | |
} else { | |
// NO, it does not have a featured image, let's import the image from YouTube | |
// Fetch the upload directory location | |
$upload_directory = wp_upload_dir(); | |
// Acertain the image filename | |
$upload_filename = basename( $youtube_video_data['thumbnail'] ); | |
// Build the upload path | |
$upload_dir_w_filename = $upload_directory['path'] . $upload_filename; | |
// Fetch the remote file | |
$remote_file = file_get_contents( $youtube_video_data['thumbnail'] ); | |
// Open a local file stream for writing | |
$save_remote_file_locally = fopen( $upload_dir_w_filename, 'w' ); | |
// Write out the remote file to our local stream | |
fwrite( $save_remote_file_locally, $remote_file ); | |
// And close the stream | |
fclose( $save_remote_file_locally ); | |
// Let's see what we've fetched.. | |
$wp_filetype = wp_check_filetype( $upload_dir_w_filename ); | |
// Let's build the new attachment arguments for insertion into the media library | |
$attachment_id = wp_insert_attachment( array( | |
'post_mime_type' => $wp_filetype['type'], | |
'post_title' => $youtube_video_data['title'], | |
'post_content' => '', | |
'post_status' => 'inherit', | |
), | |
$upload_dir_w_filename, $post->ID ); | |
// Fetch the WordPress attachment file | |
$attachment_file = get_attached_file( $attachment_id ); | |
// Ensure the WP image library is loaded | |
require_once ( ABSPATH . 'wp-admin/includes/image.php' ); | |
// Build the attachment meta data | |
$attachment_meta_data = wp_generate_attachment_metadata( $attachment_id, $attachment_file ); | |
// Bind the new meta data to the attachment | |
wp_update_attachment_metadata( $attachment_id, $attachment_meta_data ); | |
} // if has featured image | |
} // if yt image not empty | |
} // if yt image isset |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment