Last active
October 3, 2022 01:03
-
-
Save RadGH/64d74e429583c5ab390f1395971b2495 to your computer and use it in GitHub Desktop.
Get oEmbed data using WordPress oEmbed API, specifically for Video iframe
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 | |
// Example 1) Using a function. Returns an array. | |
function radgh_get_video_data( $url, $width = null, $height = null ) { | |
if ( function_exists('_wp_oembed_get_object') ) { | |
require_once( ABSPATH . WPINC . '/class-oembed.php' ); | |
} | |
$args = array(); | |
if ( $width ) $args['width'] = $width; | |
if ( $height ) $args['height'] = $height; | |
// If height is not given, but the width is, use 1080p aspect ratio. And vice versa. | |
if ( $width && !$height ) $args['height'] = $width * (1080/1920); | |
if ( !$width && $height ) $args['width'] = $height * (1920/1080); | |
$oembed = _wp_oembed_get_object(); | |
$provider = $oembed->get_provider( $url, $args ); | |
$data = $oembed->fetch( $provider, $url, $args ); | |
if ( $data ) { | |
$data = (array) $data; | |
if ( !isset($data['url']) ) $data['url'] = $url; | |
if ( !isset($data['provider']) ) $data['provider'] = $provider; | |
// Convert url to hostname, eg: "youtube" instead of "https://youtube.com/" | |
$data['provider-name'] = pathinfo( str_replace( array('www.'), '', parse_url( $url, PHP_URL_HOST )), PATHINFO_FILENAME ); | |
return $data; | |
} | |
return false; | |
} | |
$oembed_data = radgh_get_video_data( 'https://www.youtube.com/watch?v=ouDiKXYeqeY', 612, 344 ); | |
var_dump($oembed_data); | |
// Display the video embed code on the page. | |
if ( $oembed_data ) { | |
echo $oembed_data['html']; | |
} | |
// Example 2) No added functions. Returns an object. | |
$url = 'https://www.youtube.com/watch?v=ouDiKXYeqeY'; | |
$args = array( 'width' => 612, 'height' => 344 ); | |
require_once( ABSPATH . WPINC . '/class-oembed.php' ); | |
$oembed = _wp_oembed_get_object(); | |
$oembed_provider = $oembed->get_provider( $url, $args ); | |
$oembed_data = $oembed->fetch( $oembed_provider, $url, $args ); | |
var_dump($oembed_data); | |
// Display the video embed code on the page. | |
if ( $oembed_data ) { | |
echo $oembed_data->html; | |
} | |
/* | |
=== OUTPUT === | |
Array OR Object { | |
// These are added by the function in Example #1 | |
["url"]=> | |
string(26) "https://www.youtube.com/watch?v=ouDiKXYeqeY" | |
["provider"]=> | |
string(32) "http://www.youtube.com/oembed?scheme=https" | |
["provider-name"]=> | |
string(5) "youtube" | |
// These are available in both examples. | |
["html"]=> | |
string(137) "<iframe>...</iframe>" | |
["thumbnail_width"]=> | |
int(480) | |
["title"]=> | |
string(47) "DFVIDTUTS2015 [part 01] What is Dwarf Fortress?" | |
["type"]=> | |
string(5) "video" | |
["thumbnail_url"]=> | |
string(48) "https://i.ytimg.com/vi/ouDiKXYeqeY/hqdefault.jpg" | |
["thumbnail_height"]=> | |
int(360) | |
["height"]=> | |
int(344) | |
["provider_name"]=> | |
string(7) "YouTube" | |
["author_name"]=> | |
string(9) "captnduck" | |
["provider_url"]=> | |
string(24) "https://www.youtube.com/" | |
["width"]=> | |
int(612) | |
["version"]=> | |
string(3) "1.0" | |
["author_url"]=> | |
string(38) "https://www.youtube.com/user/captnduck" | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment