W3cubDocs

/WordPress

WP_Embed::shortcode( array $attr, string $url = '' )

The do_shortcode() callback function.

Description

Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of the registered embed handlers. If none of the regex matches and it’s enabled, then the URL will be given to the WP_oEmbed class.

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) (Optional) The URL attempting to be embedded.

Default value: ''

Return

(string|false) The embed HTML on success, otherwise the original URL. ->maybe_make_link() can return false on failure.

Source

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

public function shortcode( $attr, $url = '' ) {
		$post = get_post();

		if ( empty( $url ) && ! empty( $attr['src'] ) ) {
			$url = $attr['src'];
		}

		$this->last_url = $url;

		if ( empty( $url ) ) {
			$this->last_attr = $attr;
			return '';
		}

		$rawattr = $attr;
		$attr    = wp_parse_args( $attr, wp_embed_defaults( $url ) );

		$this->last_attr = $attr;

		// KSES converts & into & and we need to undo this.
		// See https://core.trac.wordpress.org/ticket/11311
		$url = str_replace( '&', '&', $url );

		// Look for known internal handlers.
		$embed_handler_html = $this->get_embed_handler_html( $rawattr, $url );
		if ( false !== $embed_handler_html ) {
			return $embed_handler_html;
		}

		$post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null;

		// Potentially set by WP_Embed::cache_oembed().
		if ( ! empty( $this->post_ID ) ) {
			$post_ID = $this->post_ID;
		}

		// Check for a cached result (stored as custom post or in the post meta).
		$key_suffix    = md5( $url . serialize( $attr ) );
		$cachekey      = '_oembed_' . $key_suffix;
		$cachekey_time = '_oembed_time_' . $key_suffix;

		/**
		 * Filters the oEmbed TTL value (time to live).
		 *
		 * @since 4.0.0
		 *
		 * @param int    $time    Time to live (in seconds).
		 * @param string $url     The attempted embed URL.
		 * @param array  $attr    An array of shortcode attributes.
		 * @param int    $post_ID Post ID.
		 */
		$ttl = apply_filters( 'oembed_ttl', DAY_IN_SECONDS, $url, $attr, $post_ID );

		$cache      = '';
		$cache_time = 0;

		$cached_post_id = $this->find_oembed_post_id( $key_suffix );

		if ( $post_ID ) {
			$cache      = get_post_meta( $post_ID, $cachekey, true );
			$cache_time = get_post_meta( $post_ID, $cachekey_time, true );

			if ( ! $cache_time ) {
				$cache_time = 0;
			}
		} elseif ( $cached_post_id ) {
			$cached_post = get_post( $cached_post_id );

			$cache      = $cached_post->post_content;
			$cache_time = strtotime( $cached_post->post_modified_gmt );
		}

		$cached_recently = ( time() - $cache_time ) < $ttl;

		if ( $this->usecache || $cached_recently ) {
			// Failures are cached. Serve one if we're using the cache.
			if ( '{{unknown}}' === $cache ) {
				return $this->maybe_make_link( $url );
			}

			if ( ! empty( $cache ) ) {
				/**
				 * Filters the cached oEmbed HTML.
				 *
				 * @since 2.9.0
				 *
				 * @see WP_Embed::shortcode()
				 *
				 * @param string|false $cache   The cached HTML result, stored in post meta.
				 * @param string       $url     The attempted embed URL.
				 * @param array        $attr    An array of shortcode attributes.
				 * @param int          $post_ID Post ID.
				 */
				return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID );
			}
		}

		/**
		 * Filters whether to inspect the given URL for discoverable link tags.
		 *
		 * @since 2.9.0
		 * @since 4.4.0 The default value changed to true.
		 *
		 * @see WP_oEmbed::discover()
		 *
		 * @param bool $enable Whether to enable `<link>` tag discovery. Default true.
		 */
		$attr['discover'] = apply_filters( 'embed_oembed_discover', true );

		// Use oEmbed to get the HTML.
		$html = wp_oembed_get( $url, $attr );

		if ( $post_ID ) {
			if ( $html ) {
				update_post_meta( $post_ID, $cachekey, $html );
				update_post_meta( $post_ID, $cachekey_time, time() );
			} elseif ( ! $cache ) {
				update_post_meta( $post_ID, $cachekey, '{{unknown}}' );
			}
		} else {
			$has_kses = false !== has_filter( 'content_save_pre', 'wp_filter_post_kses' );

			if ( $has_kses ) {
				// Prevent KSES from corrupting JSON in post_content.
				kses_remove_filters();
			}

			$insert_post_args = array(
				'post_name'   => $key_suffix,
				'post_status' => 'publish',
				'post_type'   => 'oembed_cache',
			);

			if ( $html ) {
				if ( $cached_post_id ) {
					wp_update_post(
						wp_slash(
							array(
								'ID'           => $cached_post_id,
								'post_content' => $html,
							)
						)
					);
				} else {
					wp_insert_post(
						wp_slash(
							array_merge(
								$insert_post_args,
								array(
									'post_content' => $html,
								)
							)
						)
					);
				}
			} elseif ( ! $cache ) {
				wp_insert_post(
					wp_slash(
						array_merge(
							$insert_post_args,
							array(
								'post_content' => '{{unknown}}',
							)
						)
					)
				);
			}

			if ( $has_kses ) {
				kses_init_filters();
			}
		}

		// If there was a result, return it.
		if ( $html ) {
			/** This filter is documented in wp-includes/class-wp-embed.php */
			return apply_filters( 'embed_oembed_html', $html, $url, $attr, $post_ID );
		}

		// Still unknown.
		return $this->maybe_make_link( $url );
	}

© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_embed/shortcode