Skip to content

Instantly share code, notes, and snippets.

@devinsays
Created June 15, 2015 22:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinsays/ec7461d30666bf2d5191 to your computer and use it in GitHub Desktop.
Save devinsays/ec7461d30666bf2d5191 to your computer and use it in GitHub Desktop.
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