get_rest_url()
云策文档标注
概述
get_rest_url() 函数用于获取站点上 REST 端点的完整 URL。它支持多站点环境,并处理不同的固定链接设置,返回的 URL 未经过转义。
关键要点
- 函数返回 REST 端点的完整 URL,适用于 WordPress REST API 开发。
- 参数包括可选的 $blog_id(博客 ID)、$path(REST 路由)和 $scheme(净化方案),默认值分别为 null、'/' 和 'rest'。
- 内部逻辑根据是否启用固定链接和多站点设置,动态构建 URL,并考虑 SSL 和管理员强制 HTTPS 情况。
- 通过 'rest_url' 过滤器允许自定义返回的 URL。
代码示例
$endpoint = get_rest_url(null, 'wp/v2/posts');
get_rest_url(); // 返回 https://example.com/wp-json/
get_rest_url(null, 'wp/v2/search'); // 返回 https://example.com/wp-json/wp/v2/search注意事项
- 返回的 URL 未经过转义,使用时需注意安全处理。
- 在 JavaScript 中,可通过查询 link 标签的 href 属性获取 REST 端点,例如:var endpoint = document.querySelector('link[rel="https://api.w.org/"]').href;
原文内容
Retrieves the URL to a REST endpoint on a site.
Description
Note: The returned URL is NOT escaped.
Parameters
$blog_idint|nulloptional-
Blog ID. Default of null returns URL for current blog.
Default:
null $pathstringoptional-
REST route. Default
'/'. $schemestringoptional-
Sanitization scheme. Default
'rest'.
Source
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 ( ! str_ends_with( $url, 'index.php' ) ) {
$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 );
}
Hooks
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Introduced. |
Skip to note 4 content
Jerome
Relevant information: If you’re using the REST API, you’re most likely using JavaScript. You can get the REST endpoint for a site by getting the
hrefattribute of a link tag WordPress adds to the head tag of your site:var endpoint = document.querySelector('link[rel="<a href="https://api.w.org/"%5D" rel="nofollow ugc">https://api.w.org/"%5D</a>').href;That code will extract the endpoint from the tag:
<link rel="<a href="https://api.w.org/"" rel="nofollow ugc">https://api.w.org/"</a>; href="<a href="https://example.com/wp-json/"" rel="nofollow ugc">https://example.com/wp-json/"</a>; />Skip to note 5 content
Khoi Pro
Example:
$endpoint = get_rest_url(null, 'wp/v2/posts');Skip to note 6 content
Jerome
get_rest_url(); // <a href="https://example.com/wp-json/" rel="nofollow ugc">https://example.com/wp-json/</a> get_rest_url( null, 'wp/v2/search' ); // <a href="https://example.com/wp-json/wp/v2/search" rel="nofollow ugc">https://example.com/wp-json/wp/v2/search</a>