Automatically Support Video Thumbnails in Your Plugin or Theme

Posted Aug 7, 2013

Actions and Filters are a great feature in the WordPress Plugin API that allow you to override or extend WordPress features. What you may not know is that themes and plugins can also create hooks of their own, allowing plugins and themes to easily work together.

Video Thumbnails takes advantage of filters by allowing developers to alter what content gets scanned for a video thumbnail. If you've ever used a filter before, the process should be pretty straightforward. If you haven't, learning is easy!

Inside Video Thumbnails is this line of code, which allows filters to be added to the markup to be scanned:

$markup = apply_filters( 'video_thumbnail_markup', $markup, $post_id );

So if we have a custom field we want automatically supported by Video Thumbnails, we can use this bit of code in our plugin or theme and 'mycustomfield' will automatically be included in the markup being scanned:

function custom_video_thumbnail_markup( $markup, $post_id ) {
    return get_post_meta( $post_id, 'my_custom_field', true ) . ' ' . $markup;

add_filter( 'video_thumbnail_markup', 'custom_video_thumbnail_markup', 10, 2 );

While this post specifically addresses Video Thumbnails, many plugins use their own hooks. Next time you're trying to extend or modify something inside WordPress itself or other plugins, check the code to see if there's a filter or action to tie into.