Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Replace with thumbnail via oembed_dataparse
<?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