返回介绍

get_rest_url()

发布于 2017-09-11 00:05:47 字数 4216 浏览 908 评论 0 收藏 0

get_rest_url( int $blog_id = null,  string $path = '/',  string $scheme = 'rest' )

Retrieves the URL to a REST endpoint on a site.


description

Note: The returned URL is NOT escaped.


参数

$blog_id

(int) (Optional) Blog ID. Default of null returns URL for current blog.

Default value: null

$path

(string) (Optional) REST route.

Default value: '/'

$scheme

(string) (Optional) Sanitization scheme.

Default value: 'rest'


返回值

(string) Full URL to the endpoint.


源代码

File: wp-includes/rest-api.php

function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) {
	if ( empty( $path ) ) {
		$path = '/';
	}

	if ( is_multisite() && get_blog_option( $blog_id, 'permalink_structure' ) || get_option( 'permalink_structure' ) ) {
		global $wp_rewrite;

		if ( $wp_rewrite->using_index_permalinks() ) {
			$url = get_home_url( $blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme );
		} else {
			$url = get_home_url( $blog_id, rest_get_url_prefix(), $scheme );
		}

		$url .= '/' . ltrim( $path, '/' );
	} else {
		$url = trailingslashit( get_home_url( $blog_id, '', $scheme ) );

		$path = '/' . ltrim( $path, '/' );

		$url = add_query_arg( 'rest_route', $path, $url );
	}

	if ( is_ssl() ) {
		// If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
		if ( $_SERVER['SERVER_NAME'] === parse_url( get_home_url( $blog_id ), PHP_URL_HOST ) ) {
			$url = set_url_scheme( $url, 'https' );
		}
	}

	if ( is_admin() && force_ssl_admin() ) {
		// In this situation the home URL may be http:, and `is_ssl()` may be
		// false, but the admin is served over https: (one way or another), so
		// REST API usage will be blocked by browsers unless it is also served
		// over HTTPS.
		$url = set_url_scheme( $url, 'https' );
	}

	/**
	 * Filters the REST URL.
	 *
	 * Use this filter to adjust the url returned by the get_rest_url() function.
	 *
	 * @since 4.4.0
	 *
	 * @param string $url     REST URL.
	 * @param string $path    REST route.
	 * @param int    $blog_id Blog ID.
	 * @param string $scheme  Sanitization scheme.
	 */
	return apply_filters( 'rest_url', $url, $path, $blog_id, $scheme );
}

更新日志

Versiondescription
4.4.0Introduced.

相关函数

Uses

  • wp-includes/rest-api.php: rest_url
  • wp-includes/rest-api.php: rest_get_url_prefix()
  • wp-includes/formatting.php: trailingslashit()
  • wp-includes/load.php: is_multisite()
  • wp-includes/load.php: is_admin()
  • wp-includes/load.php: is_ssl()
  • wp-includes/functions.php: force_ssl_admin()
  • wp-includes/functions.php: add_query_arg()
  • wp-includes/link-template.php: set_url_scheme()
  • wp-includes/link-template.php: get_home_url()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: get_option()
  • wp-includes/class-wp-rewrite.php: WP_Rewrite::using_index_permalinks()
  • wp-includes/ms-blogs.php: get_blog_option()
  • Show 9 more uses Hide more uses

Used By

  • wp-includes/rest-api.php: rest_output_rsd()
  • wp-includes/rest-api.php: rest_output_link_wp_head()
  • wp-includes/rest-api.php: rest_output_link_header()
  • wp-includes/rest-api.php: rest_url()
  • wp-includes/rest-api/class-wp-rest-server.php: WP_REST_Server::serve_request()
  • wp-includes/script-loader.php: wp_default_scripts()
  • Show 1 more used by Hide more used by

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文