Created
June 15, 2015 22:49
-
-
Save devinsays/ec7461d30666bf2d5191 to your computer and use it in GitHub Desktop.
Efficient Script Loading for oEmbeds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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