WP_oEmbed::fetch() WordPress Method

The WP_oEmbed::fetch() method is used to retrieve the HTML for an oEmbed resource. This is useful for displaying embedded content from other sites, such as videos from YouTube or tweets from Twitter.

WP_oEmbed::fetch( string $provider, string $url, string|array $args = '' ) #

Connects to a oEmbed provider and returns the result.


Parameters

$provider

(string)(Required)The URL to the oEmbed provider.

$url

(string)(Required)The URL to the content that is desired to be embedded.

$args

(string|array)(Optional) Additional arguments for retrieving embed HTML. See wp_oembed_get() for accepted arguments.

Default value: ''


Top ↑

Return

(object|false) The result in the form of an object on success, false on failure.


Top ↑

Source

File: wp-includes/class-wp-oembed.php

	public function fetch( $provider, $url, $args = '' ) {
		$args = wp_parse_args( $args, wp_embed_defaults( $url ) );

		$provider = add_query_arg( 'maxwidth', (int) $args['width'], $provider );
		$provider = add_query_arg( 'maxheight', (int) $args['height'], $provider );
		$provider = add_query_arg( 'url', urlencode( $url ), $provider );
		$provider = add_query_arg( 'dnt', 1, $provider );

		/**
		 * Filters the oEmbed URL to be fetched.
		 *
		 * @since 2.9.0
		 * @since 4.9.0 The `dnt` (Do Not Track) query parameter was added to all oEmbed provider URLs.
		 *
		 * @param string $provider URL of the oEmbed provider.
		 * @param string $url      URL of the content to be embedded.
		 * @param array  $args     Optional. Additional arguments for retrieving embed HTML.
		 *                         See wp_oembed_get() for accepted arguments. Default empty.
		 */
		$provider = apply_filters( 'oembed_fetch_url', $provider, $url, $args );

		foreach ( array( 'json', 'xml' ) as $format ) {
			$result = $this->_fetch_with_format( $provider, $format );
			if ( is_wp_error( $result ) && 'not-implemented' === $result->get_error_code() ) {
				continue;
			}
			return ( $result && ! is_wp_error( $result ) ) ? $result : false;
		}
		return false;
	}


Top ↑

Changelog

Changelog
VersionDescription
2.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.