How to set youtube thumbnail from youtube embed code as featured image in wordpress?
Asked Answered
V

2

5

How to set external url of youtube thumbnail (not uploading it inside my site) of an youtube video from iframe embed code of youtube video?

Is it possible to make function which will pull external youtube thumbnail URL and set it as featured image from:

<iframe width="420" height="315" src="www.youtube.com/embed/gTp4sZtjHS" frameborder="0" allowfullscreen></iframe>

Do not ask about OEmbed or similar things, I think question is clear.

Vassallo answered 20/7, 2013 at 6:45 Comment(2)
what will be the trigger for pulling the image from Youtube? When a user saves the post?Bawbee
Youtube ID I guess. I have that in mind, but can't put all in code.Vassallo
B
35

The format for pulling the large image from a YouTube video is this http://img.youtube.com/vi/A4a0xZMMlqE/0.jpg just replace "A4a0xZMMlqE" with the video id.

I think the easiest way to set this up would be to forget about using it as a featured image but just pulling it into the template dynamically.

So in your post, make a custom field that will hold the YouTube video ID and in the template do something like:

<img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID,'youtubeId');?>/0.jpg"/>

Does that solve your problem?

Otherwise if you're looking at really making it the featured image then you're probably looking at developing a custom plugin: http://net.tutsplus.com/tutorials/wordpress/creating-a-custom-wordpress-plugin-from-scratch/ which will most likely make use of wp_insert_attachment

I also just found media_sideload_image which I wasn't aware of. It will pull an image from a URL (i.e. the youtube link above) and attach it to a post.

Bawbee answered 20/7, 2013 at 9:39 Comment(2)
Note that https://i.ytimg.com/ is also a valid YouTube domain for fetching images.Corporeal
http://img.youtube.com/vi/A4a0xZMMlqE/maxresdefault.jpg can be used for a full resolution thumbnailSchinica
S
0

While phil's answer is nice, the link he uses is to the low resolution thumbnail. If you want a higher resolution thumbnail image you should use https://img.youtube.com/vi/{video_id}/maxresdefault.jpg

Full resolution thumbnail: https://img.youtube.com/vi/dQw4w9WgXcQ/maxresdefault.jpg

Low resolution thumbnail: http://img.youtube.com/vi/dQw4w9WgXcQ/0.jpg

As a sidenote it's also possible to get 3 more images from the video via the same API: http://img.youtube.com/vi/dQw4w9WgXcQ/1.jpg

http://img.youtube.com/vi/dQw4w9WgXcQ/2.jpg

http://img.youtube.com/vi/dQw4w9WgXcQ/3.jpg

Schinica answered 4/7, 2023 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.