Quantcast
Viewing all articles
Browse latest Browse all 15

Enable the YouTube iframe API for embedded videos

By default, videos embedded in WordPress posts are missing the enablejsapi querystring parameter that allows YouTube’s iframe API to interact with them. Fortunately you can filter the results of embedded media using the oembed_result filter. Here’s an example:

function my_youtube_player_iframe_api( $html ) {
	if ( false !== strpos( $html, 'youtube' ) ) {
		$html = str_replace( '?feature=oembed', '?feature=oembed&enablejsapi=1', $html );
	}
	return $html;
}
add_filter( 'oembed_result', 'my_youtube_player_iframe_api', 10, 1 );

OEmbed results are cached as post meta, so once you’ve modified the output you’ll have to delete previously-cached URLs before the new filter will take effect for existing posts.

Once you’ve done all of that you can configure and use the iframe API to track interactions, manipulate video controls, and do anything else that the API exposes.

The post Enable the YouTube iframe API for embedded videos appeared first on Scott Nelle.


Viewing all articles
Browse latest Browse all 15

Trending Articles