Replace with thumbnail via oembed_dataparse
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 | |
add_filter( 'oembed_dataparse', 'fwe_oembed_dataparse', 10, 3 ); | |
function fwe_oembed_dataparse( $result, $data, $url ){ | |
//Uebernommen aus wp-includes/class-oembed.php | |
//L38ff | |
$youtube_regex = array( | |
'#http://((m|www)\.)?youtube\.com/watch.*#i', | |
'#https://((m|www)\.)?youtube\.com/watch.*#i', | |
'#http://youtu\.be/.*#i', | |
'#https://youtu\.be/.*#i', | |
); | |
$is_youtube = false; | |
foreach( $youtube_regex as $regex ){ | |
if( preg_match( $regex, $url ) && ! $is_youtube ){ | |
$is_youtube = true; | |
break; | |
} | |
} | |
//Wenn es kein Youtube-Link ist | |
if( ! $is_youtube ) | |
return $result; | |
//RegEx fuer Youtube ID | |
//Quelle: http://stackoverflow.com/questions/2936467/parse-youtube-video-id-using-preg-match | |
$youtubeid_regex = '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i'; | |
if (! preg_match( $youtubeid_regex, $url, $match ) ) | |
return $result; | |
$html = '<div class="youtube-wrapper" data-youtube="' . $match[1] . '"><img src="' . esc_attr( $data->thumbnail_url ) . '" alt="' . esc_attr( $data->title ) . '" /></div>'; | |
return $html; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment