Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use oembed_default_width filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
April 22, 2023
5 minutes read

Let’s dive into the world of WordPress hooks! Today, we’re talking about the oembed_default_width filter. This nifty filter helps you control the maximum width allowed for embedded content. To get started with it, you’ll want to register it using the add_filter function. You can pop this code into your theme’s functions.php file or create a custom WordPress plugin. At WePlugins, we recommend using a custom plugin so your settings remain safe even if your theme updates.

Example 1: Basic Usage

Here’s a simple example of how to use the oembed_default_width filter. This code sets up a function that modifies the maximum width.

    function weplugins_modify_oembed_default_width($maxwidth) { 
        // Modify the $maxwidth variable as needed and return it.
        return $maxwidth; 
    }
    // Add the filter
    add_filter("oembed_default_width", "weplugins_modify_oembed_default_width", 10, 1);
    

Example 2: Conditional Width Adjustment

In this example, we show how you can conditionally modify the width based on certain criteria.

    function weplugins_conditional_oembed_width($maxwidth) { 
        if (is_home()) {
            $maxwidth = 500; // Set a different width for the homepage
        }
        return $maxwidth;
    }
    add_filter("oembed_default_width", "weplugins_conditional_oembed_width", 10, 1);
    

Example 3: Removing the Hook

If you need to remove a previously registered filter, you can do so with remove_filter.

    remove_filter("oembed_default_width", "weplugins_modify_oembed_default_width", 10, 1);
    

Make sure to provide the same callback function name, priority, and number of arguments when removing the hook callback.

If you need any customization or assistance, don’t hesitate to Contact Us.

Access Premium WordPress Plugins

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.