Hybrid Media Grabber is a standalone script for pulling media out of posts. It was created as a compliment to WordPress’ post format functionality.
As theme developers, we often need to grab media related to a post. Unfortunately, this has been hard to do over the years because the media is typically buried within
$post->post_content. This script makes it easier than ever to get that media.
Adding theme support for Hybrid Media Grabber
To enable the media grabber, you must set this feature in your theme setup function like so:
add_theme_support( 'hybrid-core-media-grabber' );
This will load the needed files for handling this functionality. But, you’ll still need to use the functionality within your theme theme.
Using the media grabber in themes
Depending on how your theme is set up, you’ll most likely want to use this for the templates that handle the “audio” and “video” post formats. By default, this functionality is meant to be used within The Loop.
This script provides a function called
hybrid_media_grabber() to be used as a template tag. It’s a wrapper for the
The most basic call to grab media is the following (for audio):
<?php echo hybrid_media_grabber( array( 'type' => 'audio' ) ); ?>
And, the following (for video):
<?php echo hybrid_media_grabber( array( 'type' => 'video' ) ); ?>
What this does is grab the media from the post and displays it. The following list is the order in which the media grabber looks for media.
- Auto embeds.
- Embedded media like
<iframe>, and other HTML elements.
- Attached media.
hybrid_media_grabber() function takes in a single parameter, which is an array of arguments. The following are the default arguments.
$defaults = array( 'post_id' => get_the_ID(), 'type' => 'video', 'before' => '', 'after' => '', 'split_media' => false, 'width' => $content_width, );
- The ID of the post to get the media for. Defaults to the current post ID in The Loop.
- The type of media to get. Current accepted values are
video. Defaults to
- HTML to display before the media output. Defaults to an empty string.
- HTML to display after the media output. Defaults to an empty string.
- Whether to split (i.e., remove) the media from the post content. Useful when displaying on single posts. Defaults to
- The width dimension for the media. The height is automatically calculated based on the original width/height ratio and the new width. This defaults to the theme’s
$content_width(must be set). Note: Spotify embeds won’t always conform to this width.
The following are the available hooks for this script.
This is a filter hook run over the arguments passed in (covered above). This will most likely be useful in child themes that need to change how the parent is using the function.
The following is how the hook looks in the source code.
apply_filters( 'hybrid_media_grabber_args', wp_parse_args( $args, $defaults ) );
The filter hook is run over the final HTML output of the media before it is returned for use.
apply_filters( 'hybrid_media_grabber_media', $this->media, $this );
This filter hook is run over the dimensions before they’re applied to the media. You must return an array of the width and height (in that order) when using this hook.
apply_filters( 'hybrid_media_grabber_dimensions', $dimensions, $media_atts, $this );