Ways to Make YouTube Videos Responsive in WordPress

in #blog6 years ago

<p dir="auto">Anyone who keeps a WordPress blog knows that to make their posts more appealing they should spice them with images, or even better, with videos. But here’s the problem: WordPress does not make the videos responsive by default – unless you are lucky enough to have a theme with built-in support for that. <p dir="auto">How about you? Are you struggling to make your YouTube videos responsive in WordPress? In this article you can find a few solutions. <h1>No embed code is needed nowadays <p dir="auto"><br />With modern versions of WordPress, if you want to insert a YouTube video in your post, you only need to paste the video URL on a line by itself and WordPress will take care of the rest. No explicit embed code is needed. <h1>But still, the video is not responsive <p dir="auto"><br />Adding width: 100% and height: auto in your CSS doesn’t help usually. Many solutions involve surrounding the YouTube code with div elements, but who wants to mess with extra HTML code in posts? I don’t! I’m lazy and I like to make post editing as smoothly as possible. <h1>The First Solution – No Plugin <p dir="auto"><br />This solution is implemented by adding some code snippets in your theme’s files. A better solution would be to create a child theme first and make all your customizations there. This way, you won’t lose them when you update your parent theme. <p dir="auto">And here are the code snippets: <p dir="auto"><strong>1. Include a filter in your theme’s functions.php file: <pre><code>add_filter('embed_oembed_html', 'wrap_embed_with_div', 10, 3); function wrap_embed_with_div($html, $url, $attr) { return "<div class=\"responsive-container\">".$html."</div>"; } <p dir="auto"><br />This will surround the embed code with a <code><div class="responsive-container"> element. <p dir="auto"><strong>2. Add some CSS code in your theme’s style.css file: <pre><code>.responsive-container { position: relative; padding-bottom: 56.25%; /* 16:9 */ padding-top: 0px; height: 0; overflow: hidden; } .responsive-container iframe, .responsive-container object, .responsive-container embed, .responsive-container video { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } <p dir="auto"><br />You can tweak the aspect ratio to match the videos by changing <strong>padding-bottom value of the container element. How do we find that value? It’s easy with a little bit of math. 9 divided by 16 is equal to 0.5625, which we can translate to 56.25%. And that holds true for all aspect ratios: 4:3 is 3 divided by 4 which equals 0.75, or 75%. 2.39:1 is 1 divided by 2.39 which equals 0.4184, or 41.84%. So simple! <p dir="auto"><strong>Before using this, make sure you really need this. Many themes may already be using this solution. <p dir="auto">The only problem is that you are stuck with one aspect ratio for all the videos in your blog. You could solve this by adding also some Javascript code. But then you also need some PHP code to enqueue the JS file and this could turn into a full-fledged plugin already. <p dir="auto">But why create a new plugin if it already exists? And so we get to the second solution. <h1>The Second Solution – Using a Plugin <p dir="auto"><br />I tried quite a few plugins to make the videos responsive, but most of them were either premium or too heavyweight. And so I ‘ve come to the conclusion that I need to develop my own plugin – which wasn’t that difficult considering all those ideas from the first solution. But I stumbled upon this one: <h2>Fluid Video Embeds – by jamie3d <p dir="auto"><br />Of course, developing my own plugin was no longer needed. Just go to <strong>Add New Plugin admin page of your blog and search in the public repository. Then install and activate it the usual way. It is very lightweight and it simply works! <p dir="auto">It automatically detects the aspect ratio of the embedded videos and is compatible with <strong>YouTube and <strong>Vimeo. What else would you ever need anyway? <p dir="auto">In the plugin’s settings page, you can tweak it and adjust to your own needs. You can even disable the default CSS and add your own in the theme’s stylesheet. <p dir="auto">There are many other plugins out there, but I think this one is doing its job just fine. Anyway, I’m currently using it without any issues. Check this post. <h2>Make it even better <p dir="auto"><br />I must warn you about a possible optimization issue, though. With the default settings, the related CSS code is dynamically generated through a call to the AJAX controller (wp-admin/admin-ajax.php). To overcome a possible load speed degradation you have two options: <ol> <li><p dir="auto">In the plugin’s settings page disable the default CSS and manually include it in your theme’s stylesheet – a link to the CSS content is conveniently provided; or <li><p dir="auto">Use an asset optimisation plugin, which will also optimize the assets for your entire website, as a bonus. <p dir="auto">I’ve chosen the second option, and as an asset optimization plugin I’m using <strong>Autoptimize. It minifies, concatenates and compresses all of your CSS and JS content and stores the result in just a couple static files for optimum performance, including the CSS code generated by <strong>“Fluid Video Embeds” plugin. It also minifies and compresses the HTML output, but without caching it. <p dir="auto">This way, the number of network roundtrips and the volume of transmitted data are drastically reduced and so does the page load speed. It is simply amazing! <h1>The Third Solution – Yet Another Plugin <p dir="auto"><br />Ok, I cannot help it! I just found out about another excellent plugin: <strong>Youtube Embed by <strong>EmbedPlus Team. This one is absolutely amazing! I cannot imagine a Youtube parameter that isn’t covered by this wonderful plugin, which is also free and GDPR compliant, by the way! Just check it out and share your opinion. <p dir="auto"><strong>PS: Now that you are aware of these solutions, just try them out and share your experience in a comment! <p dir="auto"><center><hr /><em>Posted from my blog :<br /><span><a href="https://marketingwithvladimir.com/two-ways-to-make-youtube-videos-responsive-in-wordpress/" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://marketingwithvladimir.com/two-ways-to-make-youtube-videos-responsive-in-wordpress/<hr />
Sort:  

You got a 0.44% upvote from @minnowvotes courtesy of @vladu!

This post has received a 17.23 % upvote from @boomerang.

Congratulations @vladu! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

<table><tr><td><span><img src="https://images.hive.blog/768x0/https://steemitimages.com/60x70/http://steemitboard.com/@vladu/voted.png?201901181407" srcset="https://images.hive.blog/768x0/https://steemitimages.com/60x70/http://steemitboard.com/@vladu/voted.png?201901181407 1x, https://images.hive.blog/1536x0/https://steemitimages.com/60x70/http://steemitboard.com/@vladu/voted.png?201901181407 2x" /><td>You received more than 500 upvotes. Your next target is to reach 1000 upvotes. <p dir="auto"><sub><em><a href="https://steemitboard.com/@vladu" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Click here to view your Board<br /> <sub><em>If you no longer want to receive notifications, reply to this comment with the word <code>STOP <blockquote> <p dir="auto">Support <a href="https://steemit.com/@steemitboard" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">SteemitBoard's project! <strong><a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Vote for its witness and <strong>get one more award!