How to Dynamically Change the oEmbed Width and Height in WordPress

Do you want to dynamically change the oEmbed width and height in WordPress?

By default, WordPress does a great job at embedding videos and other oEmbeds in your posts and pages. However, sometimes you may want to dynamically change the embed width and height.

In this article, we will show you how to dynamically change the oEmbed width and height in WordPress.

Dynamically changing oEmbed width and height in WordPress

Why Set Dynamic OEmbed Width and Height in WordPress

WordPress makes it easy to embed third-party content in your posts and pages using a technology called oEmbed.

This allows you to easily embed YouTube videos, tweets, TikToks, and more. The best part is that this content is not hosted on your WordPress website, which saves your server resources and improves WordPress performance.

Fixed width oEmbed in WordPress

By default, WordPress does a great job of automatically adjusting the height and width of the embedded content to fit your posts and pages.

However, some users may want to change this default behavior. For instance, you may want to set different default embed width and height for the front page and individual articles.

This comes in handy when you are using a custom homepage layout or working on your own custom theme design.

That being said, let’s take a look at how to easily set dynamic width and height for oEmbed content in WordPress.

Method 1. Setting Dynamic Embed Width and Height in WordPress

This method requires you to add custom code to your WordPress website. If you haven’t done this before, then take a look at our guide on how to easily add custom code snippets in WordPress.

For this method, we’ll use WordPress conditional tags to detect which WordPress page is being viewed and then change the oEmbed width and height defaults accordingly.

Simply add the following code to your theme’s functions.php file or 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 to detect different scenarios.

Here is another example where we change the oEmbed default width for a custom landing page.


function wpb_oembed_defaults($embed_size) {
if( is_page( 42 ) ) {
        $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');

As you can see this code simply sets a different default width and height for a specific page ID.

Method 2. Using CSS to Set Dynamic oEmbed Width and Height

WordPress automatically adds default CSS classes to different areas of your website.

These CSS classes can then be used to change the appearance of embeds in specific areas of your website.

For instance, you may find CSS classes like page-id, post-id, category, tag, and many more in your WordPress theme. You can find out these CSS classes using the Inspect tool.

CSS class for post and page

Similarly, WordPress also adds CSS classes to embed blocks in your posts and pages. Again, you’ll use the Inspect tool to find which classes are used by the embed block.

CSS classes for embed blocks

Once you have these CSS classes you can use them to set dynamic height and width for oEmbeds. For instance, in the following sample code, we are setting dynamic width and height for the Pinterest embed block on a specific post ID.

article#post-79 .wp-block-embed-pinterest {
    max-width: 200px!important;
    max-height: 400px!important;
}

You can try out your CSS code by adding custom CSS in the theme customizer. Once you are satisfied, don’t forget to save and publish your changes.

We hope these two methods helped you learn how to easily set dynamic oEmbed width and height in WordPress. You may also want to see these useful WordPress tips, tricks and hacks or check out our pick of the best social media plugins for WordPress.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.