I am constructing a WordPress web site through which an admin can add a video file from put up metabox.
After that put up is saved and the video file is hooked up to the put up, I used ffmpeg to get the screenshot on the first second of video, then save this screenshot as a JPG file in ‘tmp’ listing in theme listing.
Lastly, I wish to programmatically make this screenshot as put up featured picture. I exploit media_sideload_image perform, which usually be used to get picture from one other server and set that picture as put up featured picture and I feel this perform could possibly be used on this case additionally.
In my case, the video screenshot was efficiently saved to ‘tmp’ listing in my theme dir, however the media_sideload_image half didn’t return something for additional course of. Here is my perform:
perform create_video_featured_image($post_id, $image_uri) {
// $image_uri = 'http://localhost/demo-site/wp-content/themes/mytheme/tmp/video_thumb_952.jpg';
$media = media_sideload_image($image_uri, $post_id);
if(!empty($media) && !is_wp_error($media)){
$args = array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' => 'any',
'post_parent' => $post_id
);
// reference new picture to set as featured
$attachments = get_posts($args);
if(isset($attachments) && is_array($attachments)){
foreach($attachments as $attachment){
// seize supply of full dimension photographs (so no 300x150 nonsense in path)
$picture = wp_get_attachment_image_src($attachment->ID, 'full');
// decide if within the $media picture we created, the string of the URL exists
if(strpos($media, $picture[0]) !== false){
// in that case, we discovered our picture. set it as thumbnail
set_post_thumbnail($post_id, $attachment->ID);
// solely need one picture
break;
}
}
}
}
}
Am I lacking something or is there one other technique to work round on this case ?
Thanks very a lot !