The oembed_result WordPress PHP filter allows you to modify the HTML returned by the oEmbed provider.
Usage
add_filter('oembed_result', 'your_custom_function', 10, 3); function your_custom_function($data, $url, $args) { // your custom code here return $data; }
Parameters
- $data: (string|false) The returned oEmbed HTML (false if unsafe).
- $url: (string) URL of the content to be embedded.
- $args: (string|array) Additional arguments for retrieving embed HTML. See wp_oembed_get() for accepted arguments. Default empty.
More information
See WordPress Developer Resources: oembed_result
Examples
Change video aspect ratio
Modify the aspect ratio of an embedded YouTube video.
add_filter('oembed_result', 'change_youtube_aspect_ratio', 10, 3); function change_youtube_aspect_ratio($data, $url, $args) { if (strpos($url, 'youtube.com') !== false || strpos($url, 'youtu.be') !== false) { $data = str_replace('width="640" height="360"', 'width="640" height="480"', $data); } return $data; }
Add responsive Bootstrap class
Add the “embed-responsive” Bootstrap class to the oEmbed output.
add_filter('oembed_result', 'add_bootstrap_responsive_class', 10, 3); function add_bootstrap_responsive_class($data, $url, $args) { $data = str_replace('<iframe', '<div class="embed-responsive embed-responsive-16by9"><iframe', $data); $data = str_replace('</iframe>', '</iframe></div>', $data); return $data; }
Remove related videos from YouTube oEmbed
Remove related videos from the end of an embedded YouTube video.
add_filter('oembed_result', 'remove_youtube_related_videos', 10, 3); function remove_youtube_related_videos($data, $url, $args) { if (strpos($url, 'youtube.com') !== false || strpos($url, 'youtu.be') !== false) { $data = str_replace('?feature=oembed', '?feature=oembed&rel=0', $data); } return $data; }
Force HTTPS for oEmbed
Ensure that oEmbed content is loaded over HTTPS.
add_filter('oembed_result', 'force_https_oembed', 10, 3); function force_https_oembed($data, $url, $args) { return str_replace('http://', 'https://', $data); }
Add a custom wrapper to oEmbed content
Add a custom wrapper around the oEmbed content.
add_filter('oembed_result', 'wrap_oembed_content', 10, 3); function wrap_oembed_content($data, $url, $args) { return '<div class="my-custom-wrapper">' . $data . '</div>'; }