Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created February 9, 2013 21:05
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 chrisguitarguy/4747114 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/4747114 to your computer and use it in GitHub Desktop.
Responsive Emdbed for WordPress`
<?php
/**
* Plugin Name: Responsive Embed
* Description: Turn WordPress's OEmbed responsive.
* Author: Christopher Davis
* Author URI: http://christopherdavis.me
* License: MIT
*
* Copyright (c) 2013 Christopher Davis <http://christopherdavis.me>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* @category WordPress
* @link http://www.thejtsite.com/blog/single/responsive-youtube-videos-in-wordpress
* @author Christopher Davis <http://christopherdavis.me>
* @copyright 2013 Christopher Davis
* @license http://opensource.org/licenses/MIT MIT
*/
!defined('ABSPATH') && exit;
ResponsiveEmbed::init();
class ResponsiveEmbed
{
private static $ins = null;
private $allowed = array(
'youtube.com',
);
public static function instance()
{
if (is_null(self::$ins)) {
self::$ins = new self;
}
return self::$ins;
}
public static function init()
{
add_filter('embed_oembed_html', array(static::instance(), 'addResponsive'), 10, 2);
}
public function addResponsive($html, $url)
{
if (preg_match($this->getRegex(), $url)) {
$html = '<div class="resp-embed-wrap">' . $html . '</div>';
}
if (!has_action('wp_footer', array($this, 'css'))) {
add_action('wp_footer', array($this, 'css'));
}
return $html;
}
public function css()
{
echo "\n";
?>
<style type="text/css">
.resp-embed-wrap {
position: relative;
padding-bottom: 56.25%;
padding-top: 30px;
height: 0;
overflow: hidden;
}
.resp-embed-wrap iframe,
.resp-embed-wrap object,
.resp-embed-wrap embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
<?php
}
private function getRegex()
{
$hosts = array_map(function($h) {
return preg_quote($h, '#');
}, apply_filters('responsive_embed_allowed', $this->allowed));
return '#(' . implode($hosts, '|') . ')#ui';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment