Last active
December 15, 2015 19:43
-
-
Save chriskoelle/431b0f0cfdd54b8ccbe8 to your computer and use it in GitHub Desktop.
Wordpress oEmbed Customizations
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
<?php | |
/** | |
* Clear oEmbed Cache on save | |
* this is necessary in order to update the oembed output | |
*/ | |
function nh_clear_oembed_caches($post_id) { | |
if ( wp_is_post_revision( $post_id ) ) return; | |
global $wp_embed; | |
$wp_embed->delete_oembed_caches($post_id); | |
} | |
add_action('save_post', 'nh_clear_oembed_caches'); | |
/** | |
* Responsive oEmbed wrapper | |
*/ | |
function nh_responsive_oembed( $output, $data, $url ) { | |
if(!is_null($data->height) && !is_null($data->width)): | |
$ratio = intval($data->height) / intval($data->width) * 100 .'%'; | |
$output = sprintf('<div class="video-embed">%s<span style="padding-bottom:%s" class="aspect-ratio"></span></div>', $output, $ratio); | |
endif; | |
return $output; | |
} | |
add_filter('oembed_dataparse', 'nh_responsive_oembed', 10, 3 ); | |
/** | |
* Modify embed dimensions | |
* not necessary if using responsive oembed wrapper | |
*/ | |
function custom_embed_size() { | |
return array( 'width' => 600, 'height' => 999 ); | |
} | |
add_filter( 'embed_defaults', 'custom_embed_size' ); | |
/** | |
* Remove related videos on YouTube oEmbed videos. | |
*/ | |
function nh_oembed_no_rel( $data, $url, $args ) { | |
if ( strpos($data, 'youtube') !== FALSE ) { | |
$data = str_replace('feature=oembed', 'feature=oembed&rel=0', $data); | |
} | |
return $data; | |
} | |
add_filter('oembed_result', 'nh_oembed_no_rel', 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
oEmbed responsive wrapper styling (scss):