Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Dynamically Change the oEmbed Width and Height in WordPress

Last updated on by
Follow WPBeginner on YouTube
How to Dynamically Change the oEmbed Width and Height in WordPress

In our earlier article, we showed you how to set oEmbed max width in WordPress 3.5 using $content_width. That is a perfectly good solution for changing the oEmbed max width and height, but that change is global. In a recent project, we wanted to have different oEmbed sizes for the homepage and the single post page. In this article, we will show you how to dynamically change the oEmbed max width and height in WordPress using the conditional statements.

All you have to do is add the following function in your theme’s functions.php file or in a site-specific plugin.

//Custom oEmbed Size
function wpb_oembed_defaults($embed_size) {
if(is_front_page()) {
        $embed_size['width'] = 940;
        $embed_size['height'] = 600;
else {
	$embed_size['width'] = 600;
        $embed_size['height'] = 338;
    return $embed_size;
add_filter('embed_defaults', 'wpb_oembed_defaults');

You can use any of the available WordPress conditional tags. You can even have the oEmbed width be custom for each page if need be using custom fields. Hopefully you will find this article useful in your next project.

Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • Nick

    Can we include % in width value? ex. $embed_size[‘width’] = 100%;

  • Joseph Nirmal

    Iam using genesis child theme (news). How to expand the width of my theme.

    • WPBeginner Support

      using CSS you can expand width of your theme. For more specific you should ask in Genesis support forums.

  • Marc

    Does “$embed_size” replace or alter “$content_width”?

    • Editorial Staff

      I think this would take priority over $content_width.

      • Marc

        I got the REQUIRED message from the theme-check plugin to set $content_width. So you are correct.

  • M Asif Rahman

    Nice and easy. Thanks.

  • Gregg

    What if you wanted text to flow around the oembed? alignleft, alignright