Using WordPress ‘get_oembed_response_data()’ PHP function

The get_oembed_response_data() WordPress PHP function retrieves the oEmbed response data for a given post.

Usage

get_oembed_response_data($post, $width);

Custom example:
Input:

get_oembed_response_data(42, 600);

Output:

Array with oEmbed response data for post with ID 42 and requested width of 600.

Parameters

  • $post (WP_Post|int): Required. Post ID or post object.
  • $width (int): Required. The requested width.

More information

See WordPress Developer Resources: get_oembed_response_data()

Examples

Get oEmbed data for a specific post

This example retrieves the oEmbed data for a post with ID 42 and a requested width of 600 pixels.

// Retrieve oEmbed data for post with ID 42
$post_id = 42;
$requested_width = 600;
$oembed_data = get_oembed_response_data($post_id, $requested_width);

Display oEmbed data as JSON

This example retrieves the oEmbed data for a post and displays it as a JSON string.

// Retrieve oEmbed data for post with ID 42
$post_id = 42;
$requested_width = 600;
$oembed_data = get_oembed_response_data($post_id, $requested_width);

// Convert oEmbed data to JSON and display it
echo json_encode($oembed_data);

Get oEmbed data for the current post in the loop

This example retrieves the oEmbed data for the current post in the WordPress loop.

// Get the current post object in the loop
$current_post = get_post();

// Retrieve oEmbed data for the current post
$requested_width = 600;
$oembed_data = get_oembed_response_data($current_post, $requested_width);

Check if a post has oEmbed data

This example checks if a post has oEmbed data available.

// Retrieve oEmbed data for post with ID 42
$post_id = 42;
$requested_width = 600;
$oembed_data = get_oembed_response_data($post_id, $requested_width);

// Check if the post has oEmbed data
if (!empty($oembed_data)) {
    echo "Post has oEmbed data";
} else {
    echo "Post does not have oEmbed data";
}

Get oEmbed data for a list of posts

This example retrieves the oEmbed data for a list of post IDs and stores it in an array.

// List of post IDs
$post_ids = array(42, 43, 44);

// Retrieve oEmbed data for each post ID
$requested_width = 600;
$oembed_data_list = array();

foreach ($post_ids as $post_id) {
    $oembed_data = get_oembed_response_data($post_id, $requested_width);
    $oembed_data_list[$post_id] = $oembed_data;
}