函数文档

get_oembed_endpoint_url()

💡 云策文档标注

概述

get_oembed_endpoint_url() 函数用于获取指定固定链接的 oEmbed 端点 URL。它支持通过参数自定义响应格式,并提供了过滤器钩子以允许开发者修改返回的 URL。

关键要点

  • 函数返回一个字符串,表示 oEmbed 端点 URL。
  • 参数 $permalink 可选,默认为空字符串;若为空,则返回基础端点 URL。
  • 参数 $format 可选,默认为 'json',用于指定响应格式。
  • 内部使用 rest_url() 和 add_query_arg() 构建 URL。
  • 提供 apply_filters('oembed_endpoint_url', $url, $permalink, $format) 钩子,允许过滤 URL。
  • 自 WordPress 4.4.0 版本引入。

代码示例

function get_oembed_endpoint_url( $permalink = '', $format = 'json' ) {
    $url = rest_url( 'oembed/1.0/embed' );

    if ( '' !== $permalink ) {
        $url = add_query_arg(
            array(
                'url'    => urlencode( $permalink ),
                'format' => ( 'json' !== $format ) ? $format : false,
            ),
            $url
        );
    }

    return apply_filters( 'oembed_endpoint_url', $url, $permalink, $format );
}

📄 原文内容

Retrieves the oEmbed endpoint URL for a given permalink.

Description

Pass an empty string as the first argument to get the endpoint base URL.

Parameters

$permalinkstringoptional
The permalink used for the url query arg. Default empty.
$formatstringoptional
The requested response format. Default 'json'.

Return

string The oEmbed endpoint URL.

Source

function get_oembed_endpoint_url( $permalink = '', $format = 'json' ) {
	$url = rest_url( 'oembed/1.0/embed' );

	if ( '' !== $permalink ) {
		$url = add_query_arg(
			array(
				'url'    => urlencode( $permalink ),
				'format' => ( 'json' !== $format ) ? $format : false,
			),
			$url
		);
	}

	/**
	 * 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 );
}

Hooks

apply_filters( ‘oembed_endpoint_url’, string $url, string $permalink, string $format )

Filters the oEmbed endpoint URL.

Changelog

Version Description
4.4.0 Introduced.