Skip to content

Instantly share code, notes, and snippets.

@pixelwatt
Last active June 2, 2017 20:01
Show Gist options
  • Save pixelwatt/7dedbe9024016014757c7387ff0ecd10 to your computer and use it in GitHub Desktop.
Save pixelwatt/7dedbe9024016014757c7387ff0ecd10 to your computer and use it in GitHub Desktop.
Use the built-in Wordpress oEmbed class to get a full object back instead of just the embed code
<?php
/**
* Import the Wordpress oEmbed class into a function in functions.php to retreive a full object instead of just the source code
* @author Rob Clark
*
*
* What's returned: (May vary by API)
*
* $embed_data->author_url
* $embed_data->author_name
* $embed_data->height
* $embed_data->title
* $embed_data->provider_name
* $embed_data->thumbnail_url
* $embed_data->version
* $embed_data->type
* $embed_data->html
* $embed_data->thumbnail_height
* $embed_data->provider_url
* $embed_data->thumbnail_width
*
*/
function my_random_function() {
// Imports the oEmbed class, because the $wp_embed global variable is lame and won't return all the oembed fields
require_once( ABSPATH . '/wp-includes/class-oembed.php' );
// Create new oEmbed object
$myembed = new WP_oEmbed;
// Determine the video provider
$provider = $myembed->discover($my_video_url);
// Now that we have the provider, we'll run it and the URL
$embed_data = $myembed->fetch( $provider, $my_video_url);
// Now we'll start using returned API data. First, let's grab the embed code.
$some_meta_or_return_value = $embed_data->html;
// Do other stuff...
return something;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment