I have coded something to solve our common problem ;)
I have added something too if you to display your formatted embed video whatever you want (out of the post content).
I - Add these functions to your 'functions.php' file
1- Remove the youtube embed from content
function remove_video( $content ) {
$postOutput = preg_replace('/<iframe[^>]+./','', $content);
return $postOutput;
}
add_filter( 'the_content', 'remove_video', 100 );
2 - Catch and return the youtube embed from content
function catch_first_youtube_video() {
global $post, $posts;
// Catch youtube iframe src
preg_match_all('/(http|https).*(yout).*/i', $post->post_content, $matches);
$youtubeUrl = $matches[0][0];
// Set the new meta for your post.
add_post_meta($post->ID, 'iFrame', $youtubeUrl)
// Remove http: or https:
$formated = preg_replace('/(http:|https:|watch\\?v=)/i','', $youtubeUrl);
// Formated string '//youtube.com/embed/XXXXXXX'
$formated =str_replace('youtube.com/','youtube.com/embed/', $formated );
return '<iframe src="'.$formated .'"></iframe>';
}
II - Call those functions in your post template content
1 - Show the post's content in your post template without the embed video
remove_filter(the_content(),'remove_video', 100 );
2 - Show the embed video iframe whatever you want to !
<?= catch_first_youtube_video(); ?>
And voilà ! Enjoy ! (Sorry for my bad english)