Created
July 17, 2014 19:44
-
-
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
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 | |
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