Skip to content

Instantly share code, notes, and snippets.

@sethrubenstein
Created July 17, 2014 19:44
Show Gist options
  • Save sethrubenstein/093762710324483ac106 to your computer and use it in GitHub Desktop.
Save sethrubenstein/093762710324483ac106 to your computer and use it in GitHub Desktop.
Get The Youtube ID from a Youtube URL in the contents of a WordPress Post
<?php
function catch_youtube_url() {
global $post, $posts;
ob_start();
ob_end_clean();
$youtube_url = '/https?:\/\/(?:www\.)?youtu(?:\.be|be\.com)\/watch(?:\?(.*?)&|\?)v=([a-zA-Z0-9_\-]+)(\S*)/i';
$url = preg_match($youtube_url, $post->post_content, $matches);
if ( $matches ){
return $matches[0];
}
}
$video_link = catch_youtube_url();
parse_str( parse_url( $video_link, PHP_URL_QUERY ), $my_array_of_vars );
echo $my_array_of_vars['v'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment