Created
November 7, 2014 01:34
-
-
Save halfempty/6b428590eadea86c508c to your computer and use it in GitHub Desktop.
WordPress vimeo ids to params and markup
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 | |
// Curl helper function | |
function curl_get($url) { | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl, CURLOPT_TIMEOUT, 30); | |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); | |
$return = curl_exec($curl); | |
curl_close($curl); | |
return $return; | |
} | |
function getVimeoId($theurl) { | |
//https://github.com/vimeo/vimeo-api-examples/blob/master/oembed/php-example.php | |
$oembed_endpoint = 'http://vimeo.com/api/oembed'; | |
// Grab the video url from the url, or use default | |
$video_url = ($_GET['url']) ? $_GET['url'] : $theurl; | |
// Create the URLs | |
$json_url = $oembed_endpoint . '.json?url=' . rawurlencode($video_url) . '&width=640'; | |
$xml_url = $oembed_endpoint . '.xml?url=' . rawurlencode($video_url) . '&width=640'; | |
// Load in the oEmbed XML | |
$oembed = simplexml_load_string(curl_get($xml_url)); | |
$theid = 'video' . $oembed->video_id; | |
return $theid; | |
} | |
function example_add_vimeo_args($provider, $url, $args) { | |
if ( strpos($provider, '//vimeo.com/') !== false ) { | |
$videoid = getVimeoId($url); | |
$provider = add_query_arg( 'api', '1', $provider ); | |
$provider = add_query_arg( 'player_id', $videoid, $provider ); | |
} | |
return $provider; | |
} | |
add_filter('oembed_fetch_url','example_add_vimeo_args',10,3); | |
function my_plugin_enable_js_api( $html, $url, $args ) { | |
if ( strstr( $html,'vimeo.com' ) ) { | |
$videoid = getVimeoId($url); | |
$newstring = 'iframe id="' . $videoid . '"'; | |
$html = str_replace( 'iframe', $newstring, $html ); | |
} | |
return $html; | |
} | |
add_filter( 'oembed_result', 'my_plugin_enable_js_api', 10, 3 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment