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

How to use oembed_min_max_width filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
October 13, 2022
5 minutes read

oembed_min_max_width filter

Filters the allowed minimum and maximum widths for the oEmbed response.

To use oembed_min_max_width filter, first you have to register it using add_filter. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.

We at WePlugins always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function modify_oembed_min_max_width_defaults which takes 1 parameter and we registered using add_filter. The first parameter oembed_min_max_width is the name of the hook, the second parameter modify_oembed_min_max_width_defaults is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometimes, you have to remove a registered hook so you can use remove_filter to remove oembed_min_max_width filter.

Parameters

    Below the 1 parameter is required to use this hook.

  • $min_max_width : (array) Minimum and maximum widths for the oEmbed response.

    ‘min’ (int) Minimum width. Default 200.

    ‘max’ (int) Maximum width. Default 600.

Live Example

apply_filters('oembed_min_max_width', array $min_max_width)

Below is an example of how you can use this hook.

Example 1: Basic Implementation

This example shows the basic implementation of the oembed_min_max_width filter.

    function weplugins_modify_oembed_min_max_width_defaults($min_max_width) { 
        // Update the $min_max_width variable according to your website requirements and return this variable. 
        // You can modify the $min_max_width variable conditionally too if you want.
        return $min_max_width; 
    }
    // add the filter
    add_filter('oembed_min_max_width', 'weplugins_modify_oembed_min_max_width_defaults', 10, 1);
    

Example 2: Custom Widths

This example modifies the default minimum and maximum widths for the oEmbed response.

    function weplugins_modify_oembed_custom_widths($min_max_width) {
        // Set custom widths
        $min_max_width['min'] = 300;
        $min_max_width['max'] = 800;
        return $min_max_width;
    }
    // add the filter
    add_filter('oembed_min_max_width', 'weplugins_modify_oembed_custom_widths', 10, 1);
    

Example 3: Conditional Widths

This example conditionally modifies the widths based on a specific post type.

    function weplugins_modify_oembed_conditional_widths($min_max_width) {
        if (is_singular('custom_post_type')) {
            $min_max_width['min'] = 400;
            $min_max_width['max'] = 900;
        }
        return $min_max_width;
    }
    // add the filter
    add_filter('oembed_min_max_width', 'weplugins_modify_oembed_conditional_widths', 10, 1);
    

Access Premium WordPress Plugins

To remove a hook callback, use the example below.

remove_filter('oembed_min_max_width', 'weplugins_modify_oembed_min_max_width_defaults', 10, 1);

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Contact Us

If you’re having any trouble using this hook or need customization, please contact our team and we’d be happy to assist you.

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.