Skip to content

Instantly share code, notes, and snippets.

@mountbatt
Last active May 17, 2024 11:10
Show Gist options
  • Save mountbatt/9cac411827e223f9cbb4b72cdffc2c14 to your computer and use it in GitHub Desktop.
Save mountbatt/9cac411827e223f9cbb4b72cdffc2c14 to your computer and use it in GitHub Desktop.
Customize WordPress Elementor Video Widget with GDPR Notice and download YT / Vimeo Thumbnails in /uploads directory for local loading
function add_custom_div_to_elementor_video_widget( $widget_content, $widget ) {
if ( 'video' === $widget->get_name() ) {
$provider = $widget->get_settings('video_type');
$provider_names = [
'youtube' => "YouTube",
'vimeo' => "Vimeo",
];
$gdpr_urls = [
'youtube' => 'https://policies.google.com/privacy?hl=de',
'vimeo' => 'https://vimeo.com/privacy',
];
$gdpr_link_stack = '<a target="_blank" rel="noopener nofollow" href="'.$gdpr_urls[$provider].'">Datenschutzerklärung</a>';
$gdpr_div = '<div class="video-provider-gdpr-info"><div class="inner">Mit dem Abspielen des Videos akzeptieren Sie die '.$gdpr_link_stack.' von '.$provider_names[$provider].'.</div></div>';
$widget_content .= $gdpr_div;
}
return $widget_content;
}
add_filter( 'elementor/widget/render_content', 'add_custom_div_to_elementor_video_widget', 10, 2 );
function get_youtube_thumbnail_with_fallback($video_id) {
$high_res_url = 'https://img.youtube.com/vi/' . $video_id . '/maxresdefault.jpg';
$low_res_url = 'https://img.youtube.com/vi/' . $video_id . '/hqdefault.jpg';
// Versuche, das Thumbnail in der höchsten Auflösung zu erhalten
$response = wp_remote_get($high_res_url);
if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) == 200) {
return $high_res_url;
} else {
// Fallback auf niedrigere Auflösung, wenn die höchste nicht verfügbar ist
return $low_res_url;
}
}
function get_video_thumbnail( $video_url ) {
$upload_dir = wp_upload_dir();
$video_thumbs_dir = $upload_dir['basedir'] . '/video-thumbs/';
// Stelle sicher, dass das Verzeichnis existiert
if ( ! file_exists( $video_thumbs_dir ) ) {
wp_mkdir_p( $video_thumbs_dir );
}
// YouTube
if ( strpos( $video_url, 'youtube.com' ) !== false || strpos( $video_url, 'youtu.be' ) !== false ) {
preg_match( '/(youtu\.be\/|youtube\.com\/(watch\?(.*&)?v=|(embed|v)\/))([^\?&"\'<>]+)/', $video_url, $matches );
if ( isset( $matches[5] ) ) {
$video_id = $matches[5];
//$thumbnail_url = 'https://img.youtube.com/vi/' . $video_id . '/hqdefault.jpg';
$thumbnail_url = get_youtube_thumbnail_with_fallback($video_id);
}
}
// Vimeo
if ( strpos( $video_url, 'vimeo.com' ) !== false ) {
$video_id = (int) substr( parse_url( $video_url, PHP_URL_PATH ), 1 );
$oembed_url = "https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/$video_id";
$response = wp_remote_get($oembed_url);
if (is_wp_error($response)) {
return false;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (isset($data['thumbnail_url'])) {
$thumbnail_url = $data['thumbnail_url']; // oder 'thumbnail_url' für das normale Thumbnail
}
}
if ( isset( $thumbnail_url ) ) {
$thumbnail_filename = $video_thumbs_dir . $video_id . '.jpg';
$thumbnail_public_url = $upload_dir['baseurl'].'/video-thumbs/'.$video_id . '.jpg';
// Überprüfe, ob das Thumbnail bereits existiert
if ( ! file_exists( $thumbnail_filename ) ) {
// Lade das Thumbnail herunter und speichere es
$image_data = file_get_contents( $thumbnail_url );
file_put_contents( $thumbnail_filename, $image_data );
}
return $thumbnail_public_url;
}
return false;
}
function adjust_elementor_video_settings( $element ) {
// Stelle sicher, dass das Element ein Widget ist
if ( 'widget' === $element->get_type() ) {
// Überprüfe, ob das Widget ein Video-Widget ist
if ( 'video' === $element->get_name() ) {
$settings = $element->get_settings();
if($element->get_settings('video_type') == 'vimeo'){
$video_url = $element->get_settings('vimeo_url');
}
if($element->get_settings('video_type') == 'youtube'){
$video_url = $element->get_settings('youtube_url');
}
if($video_url) {
$thumb = get_video_thumbnail($video_url);
}
if($thumb && $settings['image_overlay']['url'] == ""){
$settings['image_overlay']['url'] = $thumb;
$settings['image_overlay']['id'] = '';
$settings['image_overlay']['alt'] = '';
$settings['image_overlay']['source'] = '';
$element->set_settings($settings);
}
}
}
}
add_action( 'elementor/frontend/before_render', 'adjust_elementor_video_settings', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment