get_rest_url

函式
get_rest_url ( $blog_id = null, $path = '/', $scheme = 'rest' )
引數
  • (int|null) $blog_id Optional. Blog ID. Default of null returns URL for current blog.
    Required:
    Default: null
  • (string) $path Optional. REST route. Default '/'.
    Required:
    Default: '/'
  • (string) $scheme Optional. Sanitization scheme. Default 'rest'.
    Required:
    Default: 'rest'
返回值
  • (string) Full URL to the endpoint.
定義位置
相關方法
get_site_urlrest_urlget_home_urlget_sitemap_urlget_author_posts_url
引入
4.4.0
棄用
-

get_rest_url函式是一個WordPress函式,用於檢索REST API的基本URL: 這個函式接受一個可選的路徑引數,並返回與該路徑相連線的基本URL。

檢索網站上的REST端點的URL。

注意:返回的URL沒有被轉義。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) {
if ( empty( $path ) ) {
$path = '/';
}
$path = '/' . ltrim( $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 .= $path;
} else {
$url = trailingslashit( get_home_url( $blog_id, '', $scheme ) );
// nginx only allows HTTP/1.0 methods when redirecting from / to /index.php.
// To work around this, we manually add index.php to the URL, avoiding the redirect.
if ( 'index.php' !== substr( $url, 9 ) ) {
$url .= 'index.php';
}
$url = add_query_arg( 'rest_route', $path, $url );
}
if ( is_ssl() && isset( $_SERVER['SERVER_NAME'] ) ) {
// If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
if ( parse_url( get_home_url( $blog_id ), PHP_URL_HOST ) === $_SERVER['SERVER_NAME'] ) {
$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|null $blog_id Blog ID.
* @param string $scheme Sanitization scheme.
*/
return apply_filters( 'rest_url', $url, $path, $blog_id, $scheme );
}
function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) { if ( empty( $path ) ) { $path = '/'; } $path = '/' . ltrim( $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 .= $path; } else { $url = trailingslashit( get_home_url( $blog_id, '', $scheme ) ); // nginx only allows HTTP/1.0 methods when redirecting from / to /index.php. // To work around this, we manually add index.php to the URL, avoiding the redirect. if ( 'index.php' !== substr( $url, 9 ) ) { $url .= 'index.php'; } $url = add_query_arg( 'rest_route', $path, $url ); } if ( is_ssl() && isset( $_SERVER['SERVER_NAME'] ) ) { // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS. if ( parse_url( get_home_url( $blog_id ), PHP_URL_HOST ) === $_SERVER['SERVER_NAME'] ) { $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|null $blog_id Blog ID. * @param string $scheme Sanitization scheme. */ return apply_filters( 'rest_url', $url, $path, $blog_id, $scheme ); }
function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) {
	if ( empty( $path ) ) {
		$path = '/';
	}

	$path = '/' . ltrim( $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 .= $path;
	} else {
		$url = trailingslashit( get_home_url( $blog_id, '', $scheme ) );
		// nginx only allows HTTP/1.0 methods when redirecting from / to /index.php.
		// To work around this, we manually add index.php to the URL, avoiding the redirect.
		if ( 'index.php' !== substr( $url, 9 ) ) {
			$url .= 'index.php';
		}

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

	if ( is_ssl() && isset( $_SERVER['SERVER_NAME'] ) ) {
		// If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
		if ( parse_url( get_home_url( $blog_id ), PHP_URL_HOST ) === $_SERVER['SERVER_NAME'] ) {
			$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|null $blog_id Blog ID.
	 * @param string   $scheme  Sanitization scheme.
	 */
	return apply_filters( 'rest_url', $url, $path, $blog_id, $scheme );
}

常見問題

FAQs
檢視更多 >