Jetpack and Open Graph Meta Tags: change a post’s Media Type to Video

in WordPress

When sharing posts on Facebook, you may have realized that sometimes you can play a video right on Facebook, without having to click and go to another site.

Videos on Face

This is possible thanks to Open Graph Meta Tags. When you share a post on Facebook, or when Jetpack Publicize publishes a post to your Facebook page, Facebook crawls the page and looks for Open Graph meta tags in the head to build a complete post preview. That post preview will often include an image, a title, a description, and sometimes a video like on the screenshot above.

Jetpack automatically creates these Open Graph Meta tags for you unless you already use another Open Graph plugin, in which case we let the other plugin handle things.

Before to add any tags to your posts, Jetpack needs to decide whether what tags would be relevant. Is your post only an image? We’ll want to make sure that image is nicely displayed on Facebook. is your post only a video? We should try to make sure that video can be played straight from Facebook.

We consequently created rules to decide what defines an Image Post, a Gallery Post, or a Video Post. And as always, we’ve made it possible for you to customize the default rules and define your own, thanks to a filter, jetpack_media_summary_output.

Let’s take an example. Jetpack defines a Video Post as a post that includes a video embed (from YouTube, Dailymotion, Vimeo, or VideoPress). However, if that post includes more than 2 paragraphs of text, that means that the video is not the primary focus of the post; if you bothered to write some content alongside the video you probably want Facebook readers to click through and come to your site to see what you had to say.

While that works for most people, it may not be ideal for everyone. You may add 3 very little paragraphs of text below each one of the videos, and maybe that text isn’t super important. Let’s see how to use the filter to change that default behaviour.

In the example below, we’ll force Jetpack to return a “Video” type as soon as there is a video in the post, and even if there is a lot of text as well.

/**
 * Overwrite the type of post returned by Jetpack for Video posts.
 *
 * By default, Jetpack will consider a post a "Video" post
 * if it has less than 3 paragraphs of text
 * and of course an embedded video.
 * Let's change that. As long as there is a video, it's a video type.
 *
 * @param array $data    The calculated media summary data.
 * @param int   $post_id The id of the post this data applies to.
 */
function jeherve_custom_video_type( $data, $post_id ) {
	// Is there at least one video in the post?
	if ( 0 != $data['count']['video'] && ! empty( $data['video'] ) ) {
		// Let's make the post type "video".
		$data['type'] = 'video';
	}

	return $data;
}
add_filter( 'jetpack_media_summary_output', 'jeherve_custom_video_type', 10, 2 );

That’s all there is to it! You can learn more about that filter here, and you can even learn how to customize the Open Graph Meta Tags themselves in that other post of mine!

Leave a Reply