函数文档

get_oembed_response_data_for_url()

💡 云策文档标注

概述

get_oembed_response_data_for_url() 函数用于获取指定 URL 的 oEmbed 响应数据。它首先检查 URL 是否属于当前站点,如果是则返回 oEmbed 数据,否则返回 false。在 WordPress 多站点环境中,函数会处理站点切换逻辑。

关键要点

  • 函数接受两个参数:$url(字符串,必需)为要检查的 URL,$args(数组,必需)为 oEmbed 远程获取参数。
  • 返回值:如果 URL 属于当前站点,则返回 oEmbed 响应数据对象;否则返回 false。
  • 在多站点配置下,函数会通过 get_sites() 查找匹配的站点,并处理子目录或子域名安装的路径问题。
  • 使用 url_to_postid() 将 URL 转换为文章 ID,并通过 apply_filters('oembed_request_post_id', $post_id, $url) 过滤器允许修改文章 ID。
  • 函数内部调用 get_oembed_response_data() 获取最终的 oEmbed 数据,并在必要时切换和恢复博客上下文。

代码示例

function get_oembed_response_data_for_url( $url, $args ) {
    $switched_blog = false;

    if ( is_multisite() ) {
        $url_parts = wp_parse_args(
            wp_parse_url( $url ),
            array(
                'host' => '',
                'port' => null,
                'path' => '/',
            )
        );

        $qv = array(
            'domain'                 => $url_parts['host'] . ( $url_parts['port'] ? ':' . $url_parts['port'] : '' ),
            'path'                   => '/',
            'update_site_meta_cache' => false,
        );

        // In case of subdirectory configs, set the path.
        if ( ! is_subdomain_install() ) {
            $path = explode( '/', ltrim( $url_parts['path'], '/' ) );
            $path = reset( $path );

            if ( $path ) {
                $qv['path'] = get_network()->path . $path . '/';
            }
        }

        $sites = get_sites( $qv );
        $site  = reset( $sites );

        // Do not allow embeds for deleted/archived/spam sites.
        if ( ! empty( $site->deleted ) || ! empty( $site->spam ) || ! empty( $site->archived ) ) {
            return false;
        }

        if ( $site && get_current_blog_id() !== (int) $site->blog_id ) {
            switch_to_blog( $site->blog_id );
            $switched_blog = true;
        }
    }

    $post_id = url_to_postid( $url );

    /** This filter is documented in wp-includes/class-wp-oembed-controller.php */
    $post_id = apply_filters( 'oembed_request_post_id', $post_id, $url );

    if ( ! $post_id ) {
        if ( $switched_blog ) {
            restore_current_blog();
        }

        return false;
    }

    $width = isset( $args['width'] ) ? $args['width'] : 0;

    $data = get_oembed_response_data( $post_id, $width );

    if ( $switched_blog ) {
        restore_current_blog();
    }

    return $data ? (object) $data : false;
}

注意事项

  • 该函数在 WordPress 5.0.0 版本中引入。
  • 在多站点环境中,函数会检查站点状态(如已删除、垃圾或归档),如果站点无效则直接返回 false。
  • 使用 apply_filters('oembed_request_post_id', $post_id, $url) 钩子,开发者可以过滤或修改从 URL 确定的文章 ID。
  • 函数依赖于多个 WordPress 核心函数,如 get_sites()、url_to_postid() 和 get_oembed_response_data(),确保这些函数在上下文中可用。

📄 原文内容

Retrieves the oEmbed response data for a given URL.

Parameters

$urlstringrequired
The URL that should be inspected for discovery <link> tags.
$argsarrayrequired
oEmbed remote get arguments.

Return

object|false oEmbed response data if the URL does belong to the current site. False otherwise.

Source

function get_oembed_response_data_for_url( $url, $args ) {
	$switched_blog = false;

	if ( is_multisite() ) {
		$url_parts = wp_parse_args(
			wp_parse_url( $url ),
			array(
				'host' => '',
				'port' => null,
				'path' => '/',
			)
		);

		$qv = array(
			'domain'                 => $url_parts['host'] . ( $url_parts['port'] ? ':' . $url_parts['port'] : '' ),
			'path'                   => '/',
			'update_site_meta_cache' => false,
		);

		// In case of subdirectory configs, set the path.
		if ( ! is_subdomain_install() ) {
			$path = explode( '/', ltrim( $url_parts['path'], '/' ) );
			$path = reset( $path );

			if ( $path ) {
				$qv['path'] = get_network()->path . $path . '/';
			}
		}

		$sites = get_sites( $qv );
		$site  = reset( $sites );

		// Do not allow embeds for deleted/archived/spam sites.
		if ( ! empty( $site->deleted ) || ! empty( $site->spam ) || ! empty( $site->archived ) ) {
			return false;
		}

		if ( $site && get_current_blog_id() !== (int) $site->blog_id ) {
			switch_to_blog( $site->blog_id );
			$switched_blog = true;
		}
	}

	$post_id = url_to_postid( $url );

	/** This filter is documented in wp-includes/class-wp-oembed-controller.php */
	$post_id = apply_filters( 'oembed_request_post_id', $post_id, $url );

	if ( ! $post_id ) {
		if ( $switched_blog ) {
			restore_current_blog();
		}

		return false;
	}

	$width = isset( $args['width'] ) ? $args['width'] : 0;

	$data = get_oembed_response_data( $post_id, $width );

	if ( $switched_blog ) {
		restore_current_blog();
	}

	return $data ? (object) $data : false;
}

Hooks

apply_filters( ‘oembed_request_post_id’, int $post_id, string $url )

Filters the determined post ID.

Changelog

Version Description
5.0.0 Introduced.