Responsive Youtube und Vimeo Videos in Wordpress
Wenn dein Design keine responsiven Videos unterstützt, kannst du schnell selbst Unterstützung dafür einrichten.
2022-10-24 14:56:47 - Ivan
Alles was du tun musst, ist diesen Code-Schnipsel in die functions.php einzufügen:
<?php /** * Responsive Videos, Youtube and Vimeo * */ function evolution_wrap_oembed( $html ){ $html = preg_replace( '/(width|height)="\d*"\s/', "", $html ); // Strip width and height #1 return'<div class="embed-responsive embed-responsive-16by9">'.$html.'</div>'; // Wrap in div element and return #3 and #4 } add_filter( 'embed_oembed_html','evolution_wrap_oembed',10,1);
Das nötige CSS:
.embed-responsive.embed-responsive-16by9 { position: relative; padding-bottom: 56.25%; /* 16:9 */ padding-top: 25px; height: 0; } .embed-responsive.embed-responsive-16by9 iframe { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } /* Diese Angabe macht HTML5 Videos responsive */ video { width: 100% !important; height: auto !important; }
Die PHP-Funktion sorgt für die automatische Einbettung in Divs und die CSS-Spezifikationen für eine optimale Skalierung bei jeder Auflösung.