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

How to use oembed_iframe_title_attribute filter in WordPress

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

Hey there! If you’re diving into WordPress hooks, you’re in the right place. Today, let’s talk about the oembed_iframe_title_attribute filter. This hook is super useful when you want to filter the title attribute of a given oEmbed HTML iframe.

To use the oembed_iframe_title_attribute filter, first, you need to register it using add_filter. This can be done in the functions.php file of your activated theme or in a custom WordPress Plugin. At WePlugins, we always prefer creating a custom WordPress Plugin for using hooks to ensure nothing breaks when you update your WordPress Theme in the future.

In the live example below, we’ve defined a function weplugins_modify_oembed_iframe_title_attribute_defaults which takes four parameters. We’ve registered it using add_filter. The first parameter oembed_iframe_title_attribute is the name of the hook. The second parameter weplugins_modify_oembed_iframe_title_attribute_defaults is the name of the function 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 need to remove a registered hook. You can use remove_filter to remove the oembed_iframe_title_attribute filter.

Parameters

Below are the four parameters required to use this hook:

  • $title: (string) The title attribute.
  • $result: (string) The oEmbed HTML result.
  • $data: (object) A data object result from an oEmbed provider.
  • $url: (string) The URL of the content to be embedded.

Live Example 1: Basic Usage

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

function weplugins_modify_oembed_iframe_title_attribute_defaults($title, $result, $data, $url) { 
    // Update the $title variable according to your website requirements and return this variable. 
    // You can modify the $title variable conditionally too if you want.
    return $title; 
}
// add the filter
add_filter( "oembed_iframe_title_attribute", "weplugins_modify_oembed_iframe_title_attribute_defaults", 10, 4 );

Live Example 2: Conditional Title Modification

In this example, we’ll modify the title attribute based on the URL.

function weplugins_modify_oembed_iframe_title_attribute_based_on_url($title, $result, $data, $url) { 
    if (strpos($url, 'youtube.com') !== false) {
        $title = 'YouTube Video: ' . $title;
    }
    return $title; 
}
// add the filter
add_filter( "oembed_iframe_title_attribute", "weplugins_modify_oembed_iframe_title_attribute_based_on_url", 10, 4 );

Live Example 3: Remove the Hook

To remove a hook callback, use the example below.

remove_filter( "oembed_iframe_title_attribute", "weplugins_modify_oembed_iframe_title_attribute_defaults", 10, 4 );

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

Access Premium WordPress Plugins

Contact Us

Need some customization or having trouble using this hook? Contact Us 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.