Last active
November 26, 2020 04:27
-
-
Save westonruter/db093fd74797a965fbb4f3af232db47c to your computer and use it in GitHub Desktop.
Forked version of the Vidyard WordPress Plugin which adds support for AMP: https://knowledge.vidyard.com/hc/en-us/articles/360009993593-Using-the-Vidyard-for-WordPress-oEmbed-plugin
This file contains 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 | |
/* | |
Plugin Name: Vidyard WordPress Plugin | |
Plugin URI: http://vidyard.com | |
Description: Makes it easy to embed your Vidyard videos into WordPress. (Forked version which adds AMP compatibility.) | |
Version: 1.1.1 | |
Author: Vidyard | |
Author URI: http://vidyard.com | |
License: MIT | |
*/ | |
wp_oembed_add_provider( | |
'#https?://embed\.vidyard\.com/share/.*#i', | |
'https://api.vidyard.com/dashboard/v1.1/oembed', | |
true | |
); | |
wp_oembed_add_provider( | |
'#https?://play\.vidyard\.com/.*#i', | |
'https://api.vidyard.com/dashboard/v1.1/oembed', | |
true | |
); | |
wp_oembed_add_provider( | |
'#https?://.*\.hubs\.vidyard\.com/.*#i', | |
'https://api.vidyard.com/dashboard/v1.1/oembed', | |
true | |
); | |
/* | |
* Filter oEmbed response for rendering videos on AMP pages. | |
* | |
* Example oEmbed response: | |
* <script type="text/javascript" async src="https://play.vidyard.com/embed/v4.js"></script> | |
* <img style="width: 100%; margin: auto; display: block;" class="vidyard-player-embed" src="https://play.vidyard.com/LtJJqfyHztRQP2PRYoQ4gf.jpg" data-uuid="LtJJqfyHztRQP2PRYoQ4gf" data-v="4" data-type="inline" data-width="640" data-height="360"/> | |
*/ | |
add_filter( | |
'embed_oembed_html', | |
function ( $cache, $url ) { | |
$host = wp_parse_url( $url, PHP_URL_HOST ); | |
if ( ! preg_match( '/\bvidyard\.com$/', $host ) || ! function_exists( 'is_amp_endpoint' ) || ! is_amp_endpoint() ) { | |
return $cache; | |
} | |
if ( ! preg_match( '/data-uuid="(\w+)"/', $cache, $matches ) ) { | |
return $cache; | |
} | |
$uuid = $matches[1]; | |
$v = '4'; | |
if ( preg_match( '/data-v="([0-9.]+)"/', $cache, $matches ) ) { | |
$v = $matches[1]; | |
} | |
$width = 640; | |
if ( preg_match( '/data-width="(\d+)"/', $cache, $matches ) ) { | |
$width = (int) $matches[1]; | |
} | |
$height = 360; | |
if ( preg_match( '/data-height="(\d+)"/', $cache, $matches ) ) { | |
$height = (int) $matches[1]; | |
} | |
$cache = sprintf( | |
'<amp-iframe allowfullscreen class="vidyard-iframe-%1$s" src="https://play.vidyard.com/%1$s?v=%2$s&type=inline&disable_popouts=1" sandbox="allow-scripts" layout="responsive" width="%3$s" height="%4$s"><amp-img placeholder layout="fill" src="https://play.vidyard.com/%1$s.jpg"></amp-img></amp-iframe>', | |
esc_attr( $uuid ), | |
esc_attr( $v ), | |
esc_attr( $width ), | |
esc_attr( $height ) | |
); | |
return $cache; | |
}, | |
10, | |
2 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment