Title: WP_oEmbed::data2html
Published: April 25, 2014
Last modified: April 28, 2025

---

# WP_oEmbed::data2html( object $data, string $url ): string|false

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#wp--skip-link--target)

Converts a data object from [WP_oEmbed::fetch()](https://developer.wordpress.org/reference/classes/wp_oembed/fetch/)
and returns the HTML.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#parameters)󠁿

 `$data`objectrequired

A data object result from an oEmbed provider.

`$url`stringrequired

The URL to the content that is desired to be embedded.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#return)󠁿

 string|false The HTML needed to embed on success, false on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#source)󠁿

    ```php
    public function data2html( $data, $url ) {
    	if ( ! is_object( $data ) || empty( $data->type ) ) {
    		return false;
    	}

    	$return = false;

    	switch ( $data->type ) {
    		case 'photo':
    			if ( empty( $data->url ) || empty( $data->width ) || empty( $data->height ) ) {
    				break;
    			}
    			if ( ! is_string( $data->url ) || ! is_numeric( $data->width ) || ! is_numeric( $data->height ) ) {
    				break;
    			}

    			$title  = ! empty( $data->title ) && is_string( $data->title ) ? $data->title : '';
    			$return = '<a href="' . esc_url( $url ) . '"><img src="' . esc_url( $data->url ) . '" alt="' . esc_attr( $title ) . '" width="' . esc_attr( $data->width ) . '" height="' . esc_attr( $data->height ) . '" /></a>';
    			break;

    		case 'video':
    		case 'rich':
    			if ( ! empty( $data->html ) && is_string( $data->html ) ) {
    				$return = $data->html;
    			}
    			break;

    		case 'link':
    			if ( ! empty( $data->title ) && is_string( $data->title ) ) {
    				$return = '<a href="' . esc_url( $url ) . '">' . esc_html( $data->title ) . '</a>';
    			}
    			break;

    		default:
    			$return = false;
    	}

    	/**
    	 * Filters the returned oEmbed HTML.
    	 *
    	 * Use this filter to add support for custom data types, or to filter the result.
    	 *
    	 * @since 2.9.0
    	 *
    	 * @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 apply_filters( 'oembed_dataparse', $return, $data, $url );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-oembed.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-oembed.php#L698)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-oembed.php#L698-L747)

## 󠀁[Hooks](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#hooks)󠁿

 [apply_filters( ‘oembed_dataparse’, string|false $return, object $data, string $url )](https://developer.wordpress.org/reference/hooks/oembed_dataparse/)

Filters the returned oEmbed HTML.

## 󠀁[Related](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#related)󠁿

| Uses | Description | 
| [esc_url()](https://developer.wordpress.org/reference/functions/esc_url/)`wp-includes/formatting.php` |

Checks and cleans a URL.

  | 
| [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)`wp-includes/formatting.php` |

Escaping for HTML attributes.

  | 
| [esc_html()](https://developer.wordpress.org/reference/functions/esc_html/)`wp-includes/formatting.php` |

Escaping for HTML blocks.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 2 more](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#)

| Used by | Description | 
| [WP_oEmbed::get_html()](https://developer.wordpress.org/reference/classes/wp_oembed/get_html/)`wp-includes/class-wp-oembed.php` |

The do-it-all function that takes a URL and attempts to return the HTML.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/WP_oEmbed/data2html/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.9.0](https://developer.wordpress.org/reference/since/2.9.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_oembed%2Fdata2html%2F)
before being able to contribute a note or feedback.