public
Created

Responsive Emdbed for WordPress`

  • Download Gist
responsive-embed.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
<?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';
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.