Làm sao để iframe của Videos Youtube responsive trong WordPress

banner home FINAL 1050x121 1
huong dan su dung wordpress 2

Làm sao để iframe của Videos Youtube responsive trong WordPress

Thêm 1 DIV bao khoanh iFrame trong bài viết

Đoạn code này sẽ giúp chúng ta thêm 1 DIV bao quanh iframe của video youtube. Thêm vào file functions.php

<?php
/*
* Add to functions.php 
* Add div.videoWrapper to iframe
*/
function div_wrapper($content) {
// match any iframes
/*$pattern = '~<iframe.*</iframe>|<embed.*</embed>~'; // Add it if all iframe*/
$pattern = '~<iframe.*src=".*(youtube.com|youtu.be).*</iframe>|<embed.*</embed>~'; //only iframe youtube
preg_match_all($pattern, $content, $matches);
foreach ($matches[0] as $match) {
// wrap matched iframe with div
$wrappedframe = '<div class="videoWrapper">' . $match . '</div>';
//replace original iframe with new in content
$content = str_replace($match, $wrappedframe, $content);
}
return $content; 
}
add_filter('the_content', 'div_wrapper');
?>

Css để respon

Thêm đoạn code css này vào file style.css của theme

.videoWrapper {
position: relative;
padding-bottom: 56.25%; /* 16:9 */
padding-top: 25px;
height: 0;
}
.videoWrapper iframe,
.videoWrapper object,
.videoWrapper embed{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
0/5 (0 Reviews)

Theo dõi và cập nhật tin tức AZ9s thông qua các kênh truyền thông:

- Zalo Channel

- Facebook Channel

- Youtube Channel

banner home FINAL 1050x121 1
0 0 votes
Article Rating
Nhận thông báo qua Email
Nhận thông báo cho
guest

0 Comments
Inline Feedbacks
View all comments
0
Hãy để lại bình luận của bạn!x
()
x