I used WordPress’ get_the_post_thumbnail() function in order to output the current post’s featured image. It was great, until I had to link to the featured image. To do that, I needed the value of the SRC attribute of that IMG tag, i.e., the direct link to the featured image.
Thanks to phpxref, and the WordPress Codex, I managed to write a small useful function just for that. Without further ado, I present to you, the function:
function oy_get_direct_thumbnail_link( $post_id = NULL ) { global $id; $post_id = ( NULL === $post_id ) ? $id : $post_id; $src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'full'); $src = $src[0]; return $src; }
The first and only parameter ($post_id) is optional. If used inside the loop, it fetches the current post’s ID by itself.
I’ve received many emails complaining that the plugin does not recognize YouTube videos. Because of lack of time, I delayed the update until now.
I just released an update to the plugin which should solve most of the issues that I was contacted about.
Go ahead and download the new version:
http://wordpress.org/extend/plugins/youtube-thumbnailer/
I have received a request from someone that wants even more from my WordPress Custom Fields Plugin. He has a category called “videos” which has posts with YouTube videos embedded with some plugin. He wants to put the videos thumbnail as the post’s thumbnail.
First, how is the video embedded in the post? It uses the following syntax:
[youtube $video_key]
Now you can embed it like you would normally do.
Second, how do we get the thumbnail itself after we extracted the video key? We build the image’s URL with the following syntax:
http://i2.ytimg.com/vi/$video_key/default.jpg
Now, how do we put all of this together into a plugin that inserts the thumbnails?
You don’t have to because I have already done it for you.
- Visit the plugin page of YouTube Thumbnailer
- Download the plugin here
- Unzip it’s contents
- Upload the folder you unzipped to
/wp-content/plugins/using your favorite FTP client. - Go to your admin panel and activate the plugin called “Youtube Thumbnailer”
This plugin runs through the posts in your blog and if it finds a YouTube video embedded in a post it sets the thumbnail to be the video’s thumbnail.
That’s it, have fun playing with the configuration.
If you encounter any issues please comment/contact me.
