Using WordPress ‘get_oembed_response_data_for_url()’ PHP function

The get_oembed_response_data_for_url() WordPress PHP function retrieves the oEmbed response data for a given URL.

Usage

get_oembed_response_data_for_url( $url, $args );

Parameters

  • $url string (Required) – The URL that should be inspected for discovery <link> tags.
  • $args array (Required) – oEmbed remote get arguments.

More information

See WordPress Developer Resources: get_oembed_response_data_for_url

Examples

Get oEmbed data for a YouTube video

This example retrieves oEmbed data for a YouTube video and displays the video title.

// URL of the YouTube video
$video_url = 'https://www.youtube.com/watch?v=9bZkp7q19f0';

// Get oEmbed data for the video
$oembed_data = get_oembed_response_data_for_url( $video_url, array() );

// Display the video title
echo 'Video Title: ' . $oembed_data->title;

Get oEmbed data with custom width

This example retrieves oEmbed data for a Vimeo video with a custom width of 640 pixels.

// URL of the Vimeo video
$vimeo_url = 'https://vimeo.com/76979871';

// Set custom width
$args = array( 'width' => 640 );

// Get oEmbed data for the video
$oembed_data = get_oembed_response_data_for_url( $vimeo_url, $args );

// Display the custom width video
echo $oembed_data->html;

Check if oEmbed data is available

This example checks if oEmbed data is available for a URL before retrieving it.

// URL to check for oEmbed data
$url = 'https://example.com/some-page';

// Check if oEmbed data is available
if ( get_oembed_response_data_for_url( $url, array() ) ) {
    // Get oEmbed data
    $oembed_data = get_oembed_response_data_for_url( $url, array() );
    // Display the oEmbed content
    echo $oembed_data->html;
} else {
    echo 'No oEmbed data available for this URL.';
}

Get oEmbed data for a WordPress post

This example retrieves oEmbed data for a WordPress post and displays the post’s title and author name.

// URL of the WordPress post
$post_url = 'https://example.com/my-wordpress-post';

// Get oEmbed data for the post
$oembed_data = get_oembed_response_data_for_url( $post_url, array() );

// Display the post title and author name
echo 'Post Title: ' . $oembed_data->title . '<br>';
echo 'Author: ' . $oembed_data->author_name;

Get oEmbed data for a SoundCloud track

This example retrieves oEmbed data for a SoundCloud track and displays the track’s title and description.

// URL of the SoundCloud track
$track_url = 'https://soundcloud.com/forss/flickermood';

// Get oEmbed data for the track
$oembed_data = get_oembed_response_data_for_url( $track_url, array() );

// Display the track title and description
echo 'Track Title: ' . $oembed_data->title . '<br>';
echo 'Description: ' . $oembed_data->description;