WordPress at a glance

wp_oembed_get() WP 2.9.0

Embeds the object by specified URL. Tries to get the HTML code from the passed URL by using supported WordPress oEmbed providers.

oEmbed — it is an open format designed to make it easier to embed content from one web page into another. Content can be a photo, video, link, or another type of data.

This function does not cache the result, use the WP_Embed object to cache it:

global $wp_embed;
echo $wp_embed->autoembed('https://www.youtube.com/watch?v=c6afCBCzLAY&t=2s');

/* returns:
<iframe width="600" height="338" 
src="https://www.youtube.com/embed/c6afCBCzLAY?start=2&feature=oembed"
frameborder="0" 
allow="autoplay; encrypted-media" allowfullscreen></iframe>
*/
Works based on: WP_oEmbed::get_html()

No Hooks.

Return

false/String. False on failure or the embed HTML on success.

Usage

wp_oembed_get( $url, $args );
$url(string) (required)
The URL that should be embedded.
$args(array)
Additional arguments and parameters for retrieving embed HTML.
Default: ''

Examples

#1 Embed (Build-in) the URL of a supported oEmbed in WordPress

$embed_code = wp_oembed_get( 'http://www.youtube.com/watch?v=AbcDeFg123' );

Return:

<iframe width="500" height="281" src="https://www.youtube.com/embed/AbcDeFg123?feature=oembed" frameborder="0" allowfullscreen></iframe>

Note: if the video is not available, the URL will not be processed.

#2 Embedding with parameters

Embed YouTube video and specify it's width:

$embed_code = wp_oembed_get('http://www.youtube.com/watch?v=AbcDeFg123', array('width' => 400) );

Return:

<iframe width="400" height="225" src="http://www.youtube.com/embed/jr8ouxln3CE?feature=oembed" frameborder="0" allowfullscreen></iframe>

Notes

Changelog

Since 2.9.0 Introduced.

Code of wp oembed get: wp-includes/embed.php WP 5.2.2

<?php
function wp_oembed_get( $url, $args = '' ) {
	$oembed = _wp_oembed_get_object();
	return $oembed->get_html( $url, $args );
}

Related Functions

From tag: WP Embeds (embedding URL in content)

More from category: Uncategorized

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register