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

How to Dynamically Change the oEmbed Width and Height in WordPress

Last updated on by
Elegant Themes
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 »

Comments

  1. Nick says:

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

  2. Joseph Nirmal says:

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

    • WPBeginner Support says:

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

  3. Marc says:

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

  4. M Asif Rahman says:

    Nice and easy. Thanks.

  5. Gregg says:

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

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.