oembed_dataparse
Allows you to change the content (HTML) generated when URLs supported by WordPress's oEmbed format are embedded. The change occurs before caching.
Read about oEmbed here: oEmbed in WordPress.
The WP_Embed::shortcode() method handles oEmbed URLs.
Usage
add_filter( 'oembed_dataparse', 'wp_kama_oembed_dataparse_filter', 10, 3 );
/**
* Function for `oembed_dataparse` filter-hook.
*
* @param string|false $return The returned oEmbed HTML, or false on failure.
* @param object $data A data object result from an oEmbed provider.
* @param string $url The URL of the content to be embedded.
*
* @return string|false
*/
function wp_kama_oembed_dataparse_filter( $return, $data, $url ){
// filter...
return $return;
}
- $return(string)
oEmbed HTML.
Suppose a link to a YouTube video is inserted into the content:
https://www.youtube.com/watch?v=RNFRCz0whuw
When displaying the post content, WordPress makes and stores this HTTP request:
https://www.youtube.com/oembed ?maxwidth=840 &maxheight=1000 &url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DRNFRCz0whuw &dnt=1 &format=json
The response can then be filtered. The filter receives a string like this in the parameter:
<iframe width="200" height="113" src="https://www.youtube.com/embed/RNFRCz0whuw?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
- $data(object)
oEmbed object.
stdClass Object( [html] => <iframe width="200" height="113" src="https://www.youtube.com/embed/RNFRCz0whuw?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe> [thumbnail_height] => 360 [author_url] => https://www.youtube.com/channel/UC4mPz031whiFiY5ZSuXoYGg [provider_name] => YouTube [provider_url] => https://www.youtube.com/ [thumbnail_url] => https://i.ytimg.com/vi/RNFRCz0whuw/hqdefault.jpg [author_name] => wp-plus [type] => video [thumbnail_width] => 480 [width] => 200 [version] => 1.0 [height] => 113 [title] => How to create a WordPress page, post, or custom post type template — WP lessons and development )
- $url(string)
Original URL that triggered the embed.
https://www.youtube.com/watch?v=RNFRCz0whuw
Examples
#1 Responsive YouTube player
#2 Custom YouTube player output
Suppose the YouTube player must use the following markup:
<div class="basePage__video"> <div class="basePage__player"> YouTube iframe </div> <div class="basePage__caption">Video title</div> </div>
Modify the YouTube code:
add_filter( 'oembed_dataparse', function ( $return, $data ) {
if ( 'YouTube' !== $data->provider_name ) {
return $return;
}
return str_replace( "\t", '', sprintf( '
<div class="basePage__video">
<div class="basePage__player">%s</div>
<div class="basePage__caption">%s</div>
</div>
', $return, $data->title ) );
}, 10, 2 );
str_replace() is used to remove tabs from the code, which are generated by formatting in the code editor. Otherwise, processing by other functions introduces a </p> tag that could break the page layout.
The result:
Generated code:
<div class="basePage__video"> <div class="basePage__player"> <iframe title="Season Opening. Shostakovich. Symphony No. 1" width="500" height="281" src="https://www.youtube.com/embed/FCP4FtWRkPw?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </div> <div class="basePage__caption">Season Opening. Shostakovich. Symphony No. 1</div> </div>
Changelog
| Since 2.9.0 | Introduced. |
Where the hook is called
return apply_filters( 'oembed_dataparse', $return, $data, $url );
Where the hook is used in WordPress
add_filter( 'oembed_dataparse', array( $this, '_strip_newlines' ), 10, 3 );
add_filter( 'oembed_dataparse', 'wp_filter_oembed_iframe_title_attribute', 5, 3 );
add_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10, 3 );