/** * Registers an embed handler. * * Should probably only be used for sites that do not support oEmbed. * * @since 2.9.0 * * @global WP_Embed $wp_embed WordPress Embed object. * * @param string $id An internal ID/name for the handler. Needs to be unique. * @param string $regex The regex that will be used to see if this handler should be used for a URL. * @param callable $callback The callback function that will be called if the regex is matched. * @param int $priority Optional. Used to specify the order in which the registered handlers will * be tested. Default 10. */ function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) { global $wp_embed; $wp_embed->register_handler( $id, $regex, $callback, $priority ); }
/** * Unregisters a previously-registered embed handler. * * @since 2.9.0 * * @global WP_Embed $wp_embed WordPress Embed object. * * @param string $id The handler ID that should be removed. * @param int $priority Optional. The priority of the handler to be removed. Default 10. */ function wp_embed_unregister_handler( $id, $priority = 10 ) { global $wp_embed; $wp_embed->unregister_handler( $id, $priority ); }
/** * Creates default array of embed parameters. * * The width defaults to the content width as specified by the theme. If the * theme does not specify a content width, then 500px is used. * * The default height is 1.5 times the width, or 1000px, whichever is smaller. * * The {@see 'embed_defaults'} filter can be used to adjust either of these values. * * @since 2.9.0 * * @global int $content_width * * @param string $url Optional. The URL that should be embedded. Default empty. * @return int[] { * Indexed array of the embed width and height in pixels. * * @type int $0 The embed width. * @type int $1 The embed height. * } */ function wp_embed_defaults( $url = '' ) { if ( ! empty( $GLOBALS['content_width'] ) ) { $width = (int) $GLOBALS['content_width']; }
/** * Filters the default array of embed dimensions. * * @since 2.9.0 * * @param int[] $size { * Indexed array of the embed width and height in pixels. * * @type int $0 The embed width. * @type int $1 The embed height. * } * @param string $url The URL that should be embedded. */ return apply_filters( 'embed_defaults', compact( 'width', 'height' ), $url ); }
/** * Attempts to fetch the embed HTML for a provided URL using oEmbed. * * @since 2.9.0 * * @see WP_oEmbed * * @param string $url The URL that should be embedded. * @param array|string $args { * Optional. Additional arguments for retrieving embed HTML. Default empty. * * @type int|string $width Optional. The `maxwidth` value passed to the provider URL. * @type int|string $height Optional. The `maxheight` value passed to the provider URL. * @type bool $discover Optional. Determines whether to attempt to discover link tags * at the given URL for an oEmbed provider when the provider URL * is not found in the built-in providers list. Default true. * } * @return string|false The embed HTML on success, false on failure. */ function wp_oembed_get( $url, $args = '' ) { $oembed = _wp_oembed_get_object(); return $oembed->get_html( $url, $args ); }
if ( is_null( $wp_oembed ) ) { $wp_oembed = new WP_oEmbed(); } return $wp_oembed; }
/** * Adds a URL format and oEmbed provider URL pair. * * @since 2.9.0 * * @see WP_oEmbed * * @param string $format The format of URL that this provider can handle. You can use asterisks * as wildcards. * @param string $provider The URL to the oEmbed provider. * @param bool $regex Optional. Whether the `$format` parameter is in a RegEx format. Default false. */ function wp_oembed_add_provider( $format, $provider, $regex = false ) { if ( did_action( 'plugins_loaded' ) ) { $oembed = _wp_oembed_get_object(); $oembed->providers[ $format ] = array( $provider, $regex ); } else { WP_oEmbed::_add_provider_early( $format, $provider, $regex ); } }
/** * Removes an oEmbed provider. * * @since 3.5.0 * * @see WP_oEmbed * * @param string $format The URL format for the oEmbed provider to remove. * @return bool Was the provider removed successfully? */ function wp_oembed_remove_provider( $format ) { if ( did_action( 'plugins_loaded' ) ) { $oembed = _wp_oembed_get_object();
/** * Determines if default embed handlers should be loaded. * * Checks to make sure that the embeds library hasn't already been loaded. If * it hasn't, then it will load the embeds library. * * @since 2.9.0 * * @see wp_embed_register_handler() */ function wp_maybe_load_embeds() { /** * Filters whether to load the default embed handlers. * * Returning a falsey value will prevent loading the default embed handlers. * * @since 2.9.0 * * @param bool $maybe_load_embeds Whether to load the embeds library. Default true. */ if ( ! apply_filters( 'load_default_embeds', true ) ) { return; }
/** * YouTube iframe embed handler callback. * * Catches YouTube iframe embed URLs that are not parsable by oEmbed but can be translated into a URL that is. * * @since 4.0.0 * * @global WP_Embed $wp_embed WordPress Embed object. * * @param array $matches The RegEx matches from the provided regex when calling * wp_embed_register_handler(). * @param array $attr Embed attributes. * @param string $url The original URL that was matched by the regex. * @param array $rawattr The original unmodified attributes. * @return string The embed HTML. */ function wp_embed_handler_youtube( $matches, $attr, $url, $rawattr ) { global $wp_embed; $embed = $wp_embed->autoembed( sprintf( 'https://youtube.com/watch?v=%s', urlencode( $matches[2] ) ) );
/** * Filters the YouTube embed output. * * @since 4.0.0 * * @see wp_embed_handler_youtube() * * @param string $embed YouTube embed output. * @param array $attr An array of embed attributes. * @param string $url The original URL that was matched by the regex. * @param array $rawattr The original unmodified attributes. */ return apply_filters( 'wp_embed_handler_youtube', $embed, $attr, $url, $rawattr ); }
/** * Audio embed handler callback. * * @since 3.6.0 * * @param array $matches The RegEx matches from the provided regex when calling wp_embed_register_handler(). * @param array $attr Embed attributes. * @param string $url The original URL that was matched by the regex. * @param array $rawattr The original unmodified attributes. * @return string The embed HTML. */ function wp_embed_handler_audio( $matches, $attr, $url, $rawattr ) { $audio = sprintf( '[audio src="%s" /]', esc_url( $url ) );
/** * Filters the audio embed output. * * @since 3.6.0 * * @param string $audio Audio embed output. * @param array $attr An array of embed attributes. * @param string $url The original URL that was matched by the regex. * @param array $rawattr The original unmodified attributes. */ return apply_filters( 'wp_embed_handler_audio', $audio, $attr, $url, $rawattr ); }
/** * Video embed handler callback. * * @since 3.6.0 * * @param array $matches The RegEx matches from the provided regex when calling wp_embed_register_handler(). * @param array $attr Embed attributes. * @param string $url The original URL that was matched by the regex. * @param array $rawattr The original unmodified attributes. * @return string The embed HTML. */ function wp_embed_handler_video( $matches, $attr, $url, $rawattr ) { $dimensions = ''; if ( ! empty( $rawattr['width'] ) && ! empty( $rawattr['height'] ) ) { $dimensions .= sprintf( 'width="%d" ', (int) $rawattr['width'] ); $dimensions .= sprintf( 'height="%d" ', (int) $rawattr['height'] ); } $video = sprintf( '[video %s src="%s" /]', $dimensions, esc_url( $url ) );
/** * Filters the video embed output. * * @since 3.6.0 * * @param string $video Video embed output. * @param array $attr An array of embed attributes. * @param string $url The original URL that was matched by the regex. * @param array $rawattr The original unmodified attributes. */ return apply_filters( 'wp_embed_handler_video', $video, $attr, $url, $rawattr ); }
/** * Registers the oEmbed REST API route. * * @since 4.4.0 */ function wp_oembed_register_route() { $controller = new WP_oEmbed_Controller(); $controller->register_routes(); }
/** * Adds oEmbed discovery links in the head element of the website. * * @since 4.4.0 */ function wp_oembed_add_discovery_links() { $output = '';
/** * Filters the oEmbed discovery links HTML. * * @since 4.4.0 * * @param string $output HTML of the discovery links. */ echo apply_filters( 'oembed_discovery_links', $output ); }
/** * Adds the necessary JavaScript to communicate with the embedded iframes. * * This function is no longer used directly. For back-compat it exists exclusively as a way to indicate that the oEmbed * host JS _should_ be added. In `default-filters.php` there remains this code: * * add_action( 'wp_head', 'wp_oembed_add_host_js' ) * * Historically a site has been able to disable adding the oEmbed host script by doing: * * remove_action( 'wp_head', 'wp_oembed_add_host_js' ) * * In order to ensure that such code still works as expected, this function remains. There is now a `has_action()` check * in `wp_maybe_enqueue_oembed_host_js()` to see if `wp_oembed_add_host_js()` has not been unhooked from running at the * `wp_head` action. * * @since 4.4.0 * @deprecated 5.9.0 Use {@see wp_maybe_enqueue_oembed_host_js()} instead. */ function wp_oembed_add_host_js() {}
/** * Enqueue the wp-embed script if the provided oEmbed HTML contains a post embed. * * In order to only enqueue the wp-embed script on pages that actually contain post embeds, this function checks if the * provided HTML contains post embed markup and if so enqueues the script so that it will get printed in the footer. * * @since 5.9.0 * * @param string $html Embed markup. * @return string Embed markup (without modifications). */ function wp_maybe_enqueue_oembed_host_js( $html ) { if ( has_action( 'wp_head', 'wp_oembed_add_host_js' ) && preg_match( '/<blockquote\s[^>]*?wp-embedded-content/', $html ) ) { wp_enqueue_script( 'wp-embed' ); } return $html; }
/** * Retrieves the URL to embed a specific post in an iframe. * * @since 4.4.0 * * @param int|WP_Post $post Optional. Post ID or object. Defaults to the current post. * @return string|false The post embed URL on success, false if the post doesn't exist. */ function get_post_embed_url( $post = null ) { $post = get_post( $post );
/** * Filters the URL to embed a specific post. * * @since 4.4.0 * * @param string $embed_url The post embed URL. * @param WP_Post $post The corresponding post object. */ return sanitize_url( apply_filters( 'post_embed_url', $embed_url, $post ) ); }
/** * Retrieves the oEmbed endpoint URL for a given permalink. * * Pass an empty string as the first argument to get the endpoint base URL. * * @since 4.4.0 * * @param string $permalink Optional. The permalink used for the `url` query arg. Default empty. * @param string $format Optional. The requested response format. Default 'json'. * @return string The oEmbed endpoint URL. */ function get_oembed_endpoint_url( $permalink = '', $format = 'json' ) { $url = rest_url( 'oembed/1.0/embed' );
/** * Filters the oEmbed endpoint URL. * * @since 4.4.0 * * @param string $url The URL to the oEmbed endpoint. * @param string $permalink The permalink used for the `url` query arg. * @param string $format The requested response format. */ return apply_filters( 'oembed_endpoint_url', $url, $permalink, $format ); }
/** * Retrieves the embed code for a specific post. * * @since 4.4.0 * * @param int $width The width for the response. * @param int $height The height for the response. * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`. * @return string|false Embed code on success, false if post doesn't exist. */ function get_post_embed_html( $width, $height, $post = null ) { $post = get_post( $post );
/* * Note that the script must be placed after the <blockquote> and <iframe> due to a regexp parsing issue in * `wp_filter_oembed_result()`. Because of the regex pattern starts with `|(<blockquote>.*?</blockquote>)?.*|` * wherein the <blockquote> is marked as being optional, if it is not at the beginning of the string then the group * will fail to match and everything will be matched by `.*` and not included in the group. This regex issue goes * back to WordPress 4.4, so in order to not break older installs this script must come at the end. */ $output .= wp_get_inline_script_tag( file_get_contents( ABSPATH . WPINC . '/js/wp-embed' . wp_scripts_get_suffix() . '.js' ) );
/** * Filters the embed HTML output for a given post. * * @since 4.4.0 * * @param string $output The default iframe tag to display embedded content. * @param WP_Post $post Current post object. * @param int $width Width of the response. * @param int $height Height of the response. */ return apply_filters( 'embed_html', $output, $post, $width, $height ); }
/** * Retrieves the oEmbed response data for a given post. * * @since 4.4.0 * * @param WP_Post|int $post Post ID or post object. * @param int $width The requested width. * @return array|false Response data on success, false if post doesn't exist * or is not publicly viewable. */ function get_oembed_response_data( $post, $width ) { $post = get_post( $post ); $width = absint( $width );
/** * Filters the oEmbed response data. * * @since 4.4.0 * * @param array $data The response data. * @param WP_Post $post The post object. * @param int $width The requested width. * @param int $height The calculated height. */ return apply_filters( 'oembed_response_data', $data, $post, $width, $height ); }
/** * Retrieves the oEmbed response data for a given URL. * * @since 5.0.0 * * @param string $url The URL that should be inspected for discovery `<link>` tags. * @param array $args oEmbed remote get arguments. * @return object|false oEmbed response data if the URL does belong to the current site. False otherwise. */ function get_oembed_response_data_for_url( $url, $args ) { $switched_blog = false;
// In case of subdirectory configs, set the path. if ( ! is_subdomain_install() ) { $path = explode( '/', ltrim( $url_parts['path'], '/' ) ); $path = reset( $path );
/** * Ensures that the specified format is either 'json' or 'xml'. * * @since 4.4.0 * * @param string $format The oEmbed response format. Accepts 'json' or 'xml'. * @return string The format, either 'xml' or 'json'. Default 'json'. */ function wp_oembed_ensure_format( $format ) { if ( ! in_array( $format, array( 'json', 'xml' ), true ) ) { return 'json'; }
return $format; }
/** * Hooks into the REST API output to print XML instead of JSON. * * This is only done for the oEmbed API endpoint, * which supports both formats. * * @access private * @since 4.4.0 * * @param bool $served Whether the request has already been served. * @param WP_HTTP_Response $result Result to send to the client. Usually a `WP_REST_Response`. * @param WP_REST_Request $request Request used to generate the response. * @param WP_REST_Server $server Server instance. * @return true */ function _oembed_rest_pre_serve_request( $served, $result, $request, $server ) { $params = $request->get_params();
/** * Creates an XML string from a given array. * * @since 4.4.0 * @access private * * @param array $data The original oEmbed response data. * @param SimpleXMLElement $node Optional. XML node to append the result to recursively. * @return string|false XML string on success, false on error. */ function _oembed_create_xml( $data, $node = null ) { if ( ! is_array( $data ) || empty( $data ) ) { return false; }
if ( null === $node ) { $node = new SimpleXMLElement( '<oembed></oembed>' ); }
foreach ( $data as $key => $value ) { if ( is_numeric( $key ) ) { $key = 'oembed'; }
/** * Filters the given oEmbed HTML to make sure iframes have a title attribute. * * @since 5.2.0 * * @param string $result The oEmbed HTML result. * @param object $data A data object result from an oEmbed provider. * @param string $url The URL of the content to be embedded. * @return string The filtered oEmbed result. */ function wp_filter_oembed_iframe_title_attribute( $result, $data, $url ) { if ( false === $result || ! in_array( $data->type, array( 'rich', 'video' ), true ) ) { return $result; }
/** * Filters the title attribute of the given oEmbed HTML iframe. * * @since 5.2.0 * * @param string $title The title attribute. * @param string $result The oEmbed HTML result. * @param object $data A data object result from an oEmbed provider. * @param string $url The URL of the content to be embedded. */ $title = apply_filters( 'oembed_iframe_title_attribute', $title, $result, $data, $url );
/** * Filters the given oEmbed HTML. * * If the `$url` isn't on the trusted providers list, * we need to filter the HTML heavily for security. * * Only filters 'rich' and 'video' response types. * * @since 4.4.0 * * @param string $result The oEmbed HTML result. * @param object $data A data object result from an oEmbed provider. * @param string $url The URL of the content to be embedded. * @return string The filtered and sanitized oEmbed result. */ function wp_filter_oembed_result( $result, $data, $url ) { if ( false === $result || ! in_array( $data->type, array( 'rich', 'video' ), true ) ) { return $result; }
$wp_oembed = _wp_oembed_get_object();
// Don't modify the HTML for trusted providers. if ( false !== $wp_oembed->get_provider( $url, array( 'discover' => false ) ) ) { return $result; }
preg_match( '|(<blockquote>.*?</blockquote>)?.*(<iframe.*?></iframe>)|ms', $html, $content ); // We require at least the iframe to exist. if ( empty( $content[2] ) ) { return false; } $html = $content[1] . $content[2];
if ( ! empty( $content[1] ) ) { // We have a blockquote to fall back on. Hide the iframe by default. $html = str_replace( '<iframe', '<iframe style="position: absolute; visibility: hidden;"', $html ); $html = str_replace( '<blockquote', '<blockquote class="wp-embedded-content"', $html ); }
/** * Filters the string in the 'more' link displayed after a trimmed excerpt. * * Replaces '[...]' (appended to automatically generated excerpts) with an * ellipsis and a "Continue reading" link in the embed template. * * @since 4.4.0 * * @param string $more_string Default 'more' string. * @return string 'Continue reading' link prepended with an ellipsis. */ function wp_embed_excerpt_more( $more_string ) { if ( ! is_embed() ) { return $more_string; }
/** * Displays the post excerpt for the embed template. * * Intended to be used in 'The Loop'. * * @since 4.4.0 */ function the_excerpt_embed() { $output = get_the_excerpt();
/** * Filters the post excerpt for the embed template. * * @since 4.4.0 * * @param string $output The current post excerpt. */ echo apply_filters( 'the_excerpt_embed', $output ); }
/** * Filters the post excerpt for the embed template. * * Shows players for video and audio attachments. * * @since 4.4.0 * * @param string $content The current post excerpt. * @return string The modified post excerpt. */ function wp_embed_excerpt_attachment( $content ) { if ( is_attachment() ) { return prepend_attachment( '' ); }
return $content; }
/** * Enqueues embed iframe default CSS and JS. * * Enqueue PNG fallback CSS for embed iframe for legacy versions of IE. * * Allows plugins to queue scripts for the embed iframe end using wp_enqueue_script(). * Runs first in oembed_head(). * * @since 4.4.0 */ function enqueue_embed_scripts() { wp_enqueue_style( 'wp-embed-template-ie' );
/** * Fires when scripts and styles are enqueued for the embed iframe. * * @since 4.4.0 */ do_action( 'enqueue_embed_scripts' ); }
/** * Enqueues the CSS in the embed iframe header. * * @since 6.4.0 */ function wp_enqueue_embed_styles() { // Back-compat for plugins that disable functionality by unhooking this action. if ( ! has_action( 'embed_head', 'print_embed_styles' ) ) { return; } remove_action( 'embed_head', 'print_embed_styles' );
/** * Prints the JavaScript in the embed iframe header. * * @since 4.4.0 */ function print_embed_scripts() { wp_print_inline_script_tag( file_get_contents( ABSPATH . WPINC . '/js/wp-embed-template' . wp_scripts_get_suffix() . '.js' ) ); }
/** * Prepare the oembed HTML to be displayed in an RSS feed. * * @since 4.4.0 * @access private * * @param string $content The content to filter. * @return string The filtered content. */ function _oembed_filter_feed_content( $content ) { $p = new WP_HTML_Tag_Processor( $content ); while ( $p->next_tag( array( 'tag_name' => 'iframe' ) ) ) { if ( $p->has_class( 'wp-embedded-content' ) ) { $p->remove_attribute( 'style' ); } } return $p->get_updated_html(); }
/** * Prints the necessary markup for the embed comments button. * * @since 4.4.0 */ function print_embed_comments_button() { if ( is_404() || ! ( get_comments_number() || comments_open() ) ) { return; } ?> <div class="wp-embed-comments"> <a href="<?php comments_link(); ?>" target="_top"> <span class="dashicons dashicons-admin-comments"></span> <?php printf( /* translators: %s: Number of comments. */ _n( '%s <span class="screen-reader-text">Comment</span>', '%s <span class="screen-reader-text">Comments</span>', get_comments_number() ), number_format_i18n( get_comments_number() ) ); ?> </a> </div> <?php }
/** * Prints the necessary markup for the embed sharing button. * * @since 4.4.0 */ function print_embed_sharing_button() { if ( is_404() ) { return; } ?> <div class="wp-embed-share"> <button type="button" class="wp-embed-share-dialog-open" aria-label="<?php esc_attr_e( 'Open sharing dialog' ); ?>"> <span class="dashicons dashicons-share"></span> </button> </div> <?php }
/** * Prints the necessary markup for the embed sharing dialog. * * @since 4.4.0 */ function print_embed_sharing_dialog() { if ( is_404() ) { return; }
<p class="wp-embed-share-description" id="<?php echo $description_wordpress_id; ?>"> <?php _e( 'Copy and paste this URL into your WordPress site to embed' ); ?> </p> </div> <div id="<?php echo $share_tab_html_id; ?>" class="wp-embed-share-tab" role="tabpanel" aria-hidden="true"> <textarea class="wp-embed-share-input" aria-label="<?php esc_attr_e( 'HTML' ); ?>" aria-describedby="<?php echo $description_html_id; ?>" tabindex="0" readonly><?php echo esc_textarea( get_post_embed_html( 600, 400 ) ); ?></textarea>
<p class="wp-embed-share-description" id="<?php echo $description_html_id; ?>"> <?php _e( 'Copy and paste this code into your site to embed' ); ?> </p> </div> </div>
/** * Filters the site title HTML in the embed footer. * * @since 4.4.0 * * @param string $site_title The site title HTML. */ echo apply_filters( 'embed_site_title_html', $site_title ); }
/** * Filters the oEmbed result before any HTTP requests are made. * * If the URL belongs to the current site, the result is fetched directly instead of * going through the oEmbed discovery process. * * @since 4.5.3 * * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null. * @param string $url The URL that should be inspected for discovery `<link>` tags. * @param array $args oEmbed remote get arguments. * @return null|string The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. * Null if the URL does not belong to the current site. */ function wp_filter_pre_oembed_result( $result, $url, $args ) { $data = get_oembed_response_data_for_url( $url, $args );