Using WordPress ‘oembed_dataparse’ PHP filter

The oembed_dataparse WordPress PHP filter allows you to modify the returned oEmbed HTML. Use this filter to add support for custom data types, or to filter the result.

Table of contents

Usage

add_filter('oembed_dataparse', 'your_custom_function_name', 10, 3);
function your_custom_function_name($return, $data, $url) {
// your custom code here
return $return;
}

Parameters

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

More information

See WordPress Developer Resources: oembed_dataparse

Examples

Add custom styling to embedded YouTube videos

Add a custom CSS class to YouTube video iframes for custom styling.

add_filter('oembed_dataparse', 'add_youtube_custom_class', 10, 3);
function add_youtube_custom_class($return, $data, $url) {
if ('YouTube' === $data->provider_name) {
$return = str_replace('<iframe', '<iframe class="custom-youtube-class"', $return);
}

return $return;
}

Disable related video suggestions at the end of YouTube embeds.

add_filter('oembed_dataparse', 'disable_youtube_related_videos', 10, 3);
function disable_youtube_related_videos($return, $data, $url) {
if ('YouTube' === $data->provider_name) {
$return = str_replace('?feature=oembed', '?feature=oembed&rel=0', $return);
}

return $return;
}

Change the width of embedded content

Change the width of all embedded content to 800px.

add_filter('oembed_dataparse', 'change_embed_width', 10, 3);
function change_embed_width($return, $data, $url) {
$return = preg_replace('/width="\d+"/', 'width="800"', $return);
return $return;
}

Lazy load embedded content

Add the loading="lazy" attribute to iframes for better performance.

add_filter('oembed_dataparse', 'lazy_load_embeds', 10, 3);
function lazy_load_embeds($return, $data, $url) {
$return = str_replace('<iframe', '<iframe loading="lazy"', $return);
return $return;
}

Remove embedded content from specific URLs

Remove embedded content if the URL contains ‘example.com’.

add_filter('oembed_dataparse', 'remove_example_embeds', 10, 3);
function remove_example_embeds($return, $data, $url) {
if (false !== strpos($url, 'example.com')) {
return '';
}
return $return;
}

Leave a Comment

Your email address will not be published. Required fields are marked *