Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Efficient Script Loading for oEmbeds
/**
* Enqueue theme scripts
*/
function prefix_scripts() {
// FitVids Script conditionally enqueued from inc/extras.php
wp_register_script(
'prefix-fitvids',
get_template_directory_uri() . '/js/jquery.fitvids.min.js',
array( 'jquery' ),
'1.0.0',
true
);
}
add_action( 'wp_enqueue_scripts', 'prefix_scripts' );
/**
* Enqueues FitVids, since the embed might be a video.
*
* @since 1.0.0.
*
* @param string $html The generated HTML of the embed handler.
* @param string $url The embed URL.
* @param array $attr The attributes of the embed shortcode.
*
* @return string Returned HTML.
*/
function prefix_embed_container( $html, $url, $attr ) {
// Bail if this is the admin
if ( is_admin() ) {
return $html;
}
if ( isset( $attr['width'] ) ) {
wp_enqueue_script( 'prefix-fitvids' );
}
return $html;
}
add_filter( 'embed_handler_html', 'prefix_embed_container', 10, 3 );
add_filter( 'embed_oembed_html' , 'prefix_embed_container', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment