Add Drama To WordPress Posts With Video For Featured Images

Add Drama To WordPress Posts With Video For Featured Images

Replacing featured images with videos might sound gimmicky but before you dismiss the idea have a look at the New York Times Snow Fall article.

The NYT article proves that in the right template and with the right content, video can substantially enhance the storytelling.

In this article, I’ll show you how to replace featured images with videos and enable you to provide some real drama and atmosphere to your WordPress posts.

A photo of a windswept snowy lanscape.
Does this image convey the scene as well as its video equivalent?

If you haven’t looked at the NYT article yet do so now. And ask yourself this question: is the video better at conveying the desolation, wilderness and the cold?

I would suggest that it is and so in this situation, if the static image was the featured image then there is plenty to be gained in replacing the image with video.

And by only replacing the image on the post page itself, the featured image can still be used on the home page and in archive listings.

Of course, the web being the web it’s not all plain sailing. You’ve got those browsers that don’t support the HTML5 video tag at all and, unfortunately, video won’t autoplay on devices running iOS 6.1+ as Apple disabled the autoplay of videos because of concerns about bandwidth.

Whilst that’s definitely a disadvantage, it just means that these browsers just get the static image, so not all is lost.

Bearing in this in mind, if you are still keen to try replacing featured images with videos then you can download and install my featured-videos plugin.

Note: There is a plugin in the WordPress repository that also replaces featured images with videos but this asks for the embedd code in the post editing screen and is geared towards using third-party services such as YouTube and Daily Motion. I’ve concentrated on using video that is locally hosted.

How The Plugin Works

When the call is made in a template to output a featured image, the plugin intercepts the call and looks for videos in the media library with a similar name. For example, if you have used a featured image called “snow.jpg”, then “snow.mp4” or “snow.webm” will be picked up and used.

If video alternatives are found then the original HTML is wrapped in a video tag with the appropriate sources. On a browser that supports the video tag, the video will autoplay (except on iOS) whilst those browsers that don’t support the video tag will just output the img tag. Here’s some example output:

It’s worth noting that only the width is set for the video tag so that it is the same as the featured image it is replacing. The height is left off so that the video player can set the appropriate height and maintain the picture ratio.

Another fly-in-the-ointment is Chrome which has a bug – or at least my version does – where an autoplay video with no controls causes the screen to go blank. To cater for this, controls are added for Chrome which prevents the white screen and allows the video to autoplay.

The video replacement only occurs on a single item page, that is the display of a post, so home pages and archive listings that display featured images are unaffected. The video tag also only gets written out if a video file is found, otherwise the original HTML is output.

How To Use Featured Videos

  1. Install and activate the plugin.
  2. Add a featured image as per usual.
  3. Upload videos to the media library with the same name (e.g. snow.jpg = snow.mp4, snow.webm).

That’s it – the videos will automatically be added to the post. Providing a .mp4 and a .webm version will cover all major browsers.

Under The Hood

If you have no interest in the technical aspects then you can stop reading now!

The plugin hooks into the post_thumbnail_html filter to replace the standard featured image HTML with new HTML that includes a video tag and the original HTML for fallback for the non-supporting browsers.

add_filter ( 'post_thumbnail_html' , 'vfi_post_thumbnail_html' , 10 , 5);

The 5 in the arguments list ensures that all the available variables are passed to the custom function, vfi_post_thumbnail_html.

The plugin firsts checks to see if this is a single page: if it’s not then it exits and simply returns the original HTML.

if ( !is_singular() ) return $html;

It then gets details about the featured image using the wp_get_attachment_image_src and wp_get_attachment_url functions. This provides the height and width of the featured image (this may be cropped, of course), the URL of the cropped image and the URL of the original image. Having the original URL makes it easier to determine the name for matching.

/* get the basic url, width and height of the featured image */
$fi_attr = wp_get_attachment_image_src ( $post_thumbnail_id , $size);

/* get the original url - we need this to search for the video */
$fi_url = wp_get_attachment_url ( $post_thumbnail_id );

/* work out the name */
$fi_url_exploded = explode( '/' , $fi_url );
$fi_name = $fi_url_exploded[ count( $fi_url_exploded ) - 1 ];
$fi_name_exploded = explode( '.' , $fi_name );

Once the plugin has the name of the featured image, it does a meta_query search on attachments (post_type of ‘attachment’) searching _wp_attached_file for the image name (minus the extension).

/* now search the medial library for any matches */
$args = array( 'post_type' => 'attachment' , 'post_status' => 'inherit' ,
'meta_query' => array(
'key' => '_wp_attached_file',
'value' => $fi_name_exploded[0] . '.',
'compare' => 'LIKE'

$query = new WP_Query( $args );

$found = count( $query->posts );

If it finds more than one match – the original image will be picked up in this search – then it loops through and if the mime-type of the file starts with “video” then it starts to create the video tag. You’ll also see the check for whether the visitor is using Chrome, using the global $is_chrome variable, so that the controls attribute can be added.

/* loop through the matches and process those with a video mime type */
foreach( $query->posts AS $attach ){

if ( substr( $attach->post_mime_type , 0 , 5 ) == 'video' ) {

if ( $new_html == '') {

$controls = ( $is_chrome == true ) ? 'controls' : '';

$new_html = '<video width="'. $fi_attr[1] . '" class="featured" ' . $controls . ' autoplay preload loop poster="' . $fi_attr[0] . '">';

$new_html .= '<source class="' . $sourceclass . '" src="' . $attach->guid . '" type="' . $attach->post_mime_type . '">';



Once the matches have been processed, the video tag is wrapped around the original HTML and the new HTML is returned for outputting to the template. If only one match is found then the original HTML is returned.

/* if video files were found then update the html */
if ( $new_html != '' ) $html = $new_html . $html . '</video>';

Thin Line Between Atmosphere And Annoyance

This is a powerful technique but requires careful implementation. The NYT article proves that in the right setting, video can add considerably to the atmosphere of a piece and when it’s at the top of the post can really set the scene.

The key is the right setting. This is not going to work for all content on every site and just adding video for the sake of it is just going to annoy your visitors. Autoplay video has got to be of benefit.

If you can genuinely enhance the storytelling experience with video then the pay-off would seem to be considerable.

What do you think of the technique? Do you think it enhances the NYT article or is it just a gimmick?

Image credit: wackystuff