Skip to content

Instantly share code, notes, and snippets.

@chriskoelle
Last active December 15, 2015 19:43
Show Gist options
  • Save chriskoelle/431b0f0cfdd54b8ccbe8 to your computer and use it in GitHub Desktop.
Save chriskoelle/431b0f0cfdd54b8ccbe8 to your computer and use it in GitHub Desktop.
Wordpress oEmbed Customizations
<?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);
@chriskoelle
Copy link
Author

oEmbed responsive wrapper styling (scss):

.video-embed {
    margin: 20px 0;
    position: relative;

    .aspect-ratio {
        display: block;
        width: 100%;
        height: 0;
    }

    iframe {
        height: 100%;
        width: 100%;
        position: absolute;
        left: 0;
        top: 0;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment