Skip to content

Instantly share code, notes, and snippets.

@zuhra031
Forked from jlengstorf/README.md
Created October 6, 2018 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zuhra031/5419c3579f2d4c9c6cb235d2ae905e7f to your computer and use it in GitHub Desktop.
Save zuhra031/5419c3579f2d4c9c6cb235d2ae905e7f to your computer and use it in GitHub Desktop.
Adds responsive embedding to WordPress oEmbed content.

Responsive Embeds in WordPress

This snippet filters oEmbed output in WordPress (the_content()) to force responsive embeds.

Usage

To use, add the contents of responseive_embeds.less to your site's stylesheet (if you're not using LESS, don't forget to move the iframe,object,embed rule outside of .embed-container and change it to .embed-container iframe,.embed-container object,.embed-container embed).

Then add the responsive_embed() function to your theme's functions.php and insert the add_filter() call in your theme's setup function.

NOTE: If your theme is not a child theme, this can be placed directly below responsive_embed(), outside of any function.

<?php
function setup_theme( ) {
// Theme setup code...
// Filters the oEmbed process to run the responsive_embed() function
add_filter('embed_oembed_html', 'responsive_embed', 10, 3);
}
add_action('after_setup_theme', 'setup_theme');
/**
* Adds a responsive embed wrapper around oEmbed content
* @param string $html The oEmbed markup
* @param string $url The URL being embedded
* @param array $attr An array of attributes
* @return string Updated embed markup
*/
function responsive_embed($html, $url, $attr) {
return $html!=='' ? '<div class="embed-container">'.$html.'</div>' : '';
}
.embed-container {
position: relative;
padding-bottom: 56.25%;
padding-top: 30px;
height: 0;
overflow: hidden;
max-width: 100%;
height: auto;
iframe, object, embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment