WP_Embed::get_embed_handler_html() WordPress Method

The WP_Embed::get_embed_handler_html() function is used to get the HTML for a given embed handler. This function is useful for getting the HTML for an embed handler that is not registered with WordPress. For example, if you want to get the HTML for a YouTube embed handler, you can use this function. The function takes two parameters: $url (the URL of the content to be embedded) and $handler (the name of the embed handler). The $url parameter is required. The $handler parameter is optional. If you do not specify a $handler, WordPress will try to determine the best embed handler for the given $url. The function returns the HTML for the embed handler.

WP_Embed::get_embed_handler_html( array $attr, string $url ) #

Returns embed HTML for a given URL from embed handlers.


Description

Attempts to convert a URL into embed HTML by checking the URL against the regex of the registered embed handlers.


Top ↑

Parameters

$attr

(array)(Required)Shortcode attributes. Optional.

  • 'width'
    (int) Width of the embed in pixels.
  • 'height'
    (int) Height of the embed in pixels.

$url

(string)(Required)The URL attempting to be embedded.


Top ↑

Return

(string|false) The embed HTML on success, false otherwise.


Top ↑

Source

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

	public function get_embed_handler_html( $attr, $url ) {
		$rawattr = $attr;
		$attr    = wp_parse_args( $attr, wp_embed_defaults( $url ) );

		ksort( $this->handlers );
		foreach ( $this->handlers as $priority => $handlers ) {
			foreach ( $handlers as $id => $handler ) {
				if ( preg_match( $handler['regex'], $url, $matches ) && is_callable( $handler['callback'] ) ) {
					$return = call_user_func( $handler['callback'], $matches, $attr, $url, $rawattr );
					if ( false !== $return ) {
						/**
						 * Filters the returned embed HTML.
						 *
						 * @since 2.9.0
						 *
						 * @see WP_Embed::shortcode()
						 *
						 * @param string|false $return The HTML result of the shortcode, or false on failure.
						 * @param string       $url    The embed URL.
						 * @param array        $attr   An array of shortcode attributes.
						 */
						return apply_filters( 'embed_handler_html', $return, $url, $attr );
					}
				}
			}
		}

		return false;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.5.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.