各位观众老爷们,早上好/下午好/晚上好!我是你们的WordPress技术顾问,今天咱们来聊聊WordPress REST API的URL获取,特别是那个神秘的rest_get_json_url()
函数。
别担心,咱们不搞那些枯燥的源码解读,争取用最通俗易懂的方式,把这个函数扒个底朝天,让你们以后再也不怕找不到API的入口。
开场白:REST API,你得先认识它
在深入rest_get_json_url()
之前,咱们先简单回顾一下REST API是个啥玩意儿。简单来说,REST API就是一套标准的接口,允许不同的应用程序之间互相交流数据。你可以把它想象成一个翻译官,让你的网站可以和手机APP、其他网站等等无缝对接。
WordPress从4.7版本开始,内置了REST API,这意味着你可以通过标准的HTTP请求(GET, POST, PUT, DELETE)来访问和操作WordPress的内容,比如文章、分类、用户等等。
主角登场:rest_get_json_url()
闪亮登场
rest_get_json_url()
,顾名思义,就是一个用来获取JSON格式的REST API URL的函数。它位于wp-includes/rest-api.php
文件中。这个函数的作用就是根据你提供的参数,生成一个完整的、可以直接访问的API地址。
源码剖析:我们来扒它的皮
让我们来深入研究一下rest_get_json_url()
的源码,看看它到底是怎么工作的:
/**
* Retrieves the full URL to a REST endpoint.
*
* @since 4.4.0
*
* @param string $path Optional. String appended to the URL.
* @param string $scheme Optional. Scheme to use, defaults to the current scheme.
* @return string Full URL to the endpoint.
*/
function rest_get_url( $path = '/', $scheme = null ) {
$url = get_url_from_relative( rest_get_route_url( $path ), $scheme );
/**
* Filters the REST URL.
*
* @since 4.4.0
*
* @param string $url REST URL.
* @param string $path String appended to the URL.
* @param string $scheme Scheme to use.
*/
return apply_filters( 'rest_url', $url, $path, $scheme );
}
/**
* Retrieves the full URL to a REST endpoint.
*
* @since 4.4.0
*
* @param string $path Optional. String appended to the URL.
* @param string $scheme Optional. Scheme to use, defaults to the current scheme.
* @return string Full URL to the endpoint.
*/
function rest_url( $path = '/', $scheme = null ) {
return rest_get_url( $path, $scheme );
}
/**
* Retrieves the full URL to a REST endpoint.
*
* @since 4.4.0
* @deprecated 5.5.0 Use rest_url() instead.
*
* @param string $path Optional. String appended to the URL.
* @param string $scheme Optional. Scheme to use, defaults to the current scheme.
* @return string Full URL to the endpoint.
*/
function rest_get_json_url( $path = '/', $scheme = null ) {
_deprecated_function( __FUNCTION__, '5.5.0', 'rest_url()' );
return rest_url( $path, $scheme );
}
/**
* Retrieves the base URL to the REST API route.
*
* @since 4.4.0
*
* @return string Base URL to the REST API route.
*/
function rest_get_route_url( $path = '/' ) {
$url = get_option( 'siteurl' );
$url = rtrim( $url, '/' );
if ( get_option( 'permalink_structure' ) ) {
$url .= '/' . rest_get_url_prefix();
} else {
$url .= '/?' . rest_get_url_prefix();
}
$url .= '/' . ltrim( $path, '/' );
/**
* Filters the REST route URL.
*
* @since 4.4.0
*
* @param string $url REST route URL.
* @param string $path String appended to the URL.
*/
return apply_filters( 'rest_route_url', $url, $path );
}
/**
* Retrieves the REST API URL prefix.
*
* @since 4.7.0
*
* @return string REST prefix.
*/
function rest_get_url_prefix() {
/**
* Filters the REST URL prefix.
*
* @since 4.7.0
*
* @param string $prefix URL prefix. Default 'wp-json'.
*/
return apply_filters( 'rest_url_prefix', 'wp-json' );
}
敲黑板!重点来了!
rest_get_json_url( $path = '/', $scheme = null )
: 这是我们今天要研究的核心函数。但是,从 WordPress 5.5.0开始,rest_get_json_url()
函数被标记为_deprecated_function()
,这意味着它已经被废弃了,建议使用rest_url()
函数代替。rest_url( $path = '/', $scheme = null )
和rest_get_url( $path = '/', $scheme = null )
: 这两个函数实际上是相同的,rest_url()
只是rest_get_url()
的一个别名。它们都用来生成REST API的URL。rest_get_route_url( $path = '/' )
: 这个函数负责生成REST API的路由URL,也就是API的基础路径。rest_get_url_prefix()
: 这个函数返回REST API的URL前缀,默认是wp-json
。
工作流程:它是如何组装URL的?
rest_get_json_url()
(或rest_url()
) 接收两个参数:$path
(可选,附加在URL后面的路径) 和$scheme
(可选,URL的协议,例如http或https)。rest_get_json_url()
函数调用rest_url()
函数。rest_url()
函数 调用rest_get_url()
函数。rest_get_url()
函数 调用rest_get_route_url()
函数,以获取基础的REST API路由URL。rest_get_route_url()
函数,先通过get_option( 'siteurl' )
获取站点URL。rest_get_route_url()
函数,然后判断是否启用了固定链接 (get_option( 'permalink_structure' )
)。如果启用了固定链接,则将wp-json
前缀添加到站点URL后面;否则,将/?wp-json
添加到站点URL后面。rest_get_route_url()
函数,最后将传入的$path
参数添加到URL后面。rest_get_url()
函数 使用get_url_from_relative()
函数,并应用rest_url
过滤器。- 最终,
rest_get_json_url()
(或rest_url()
) 函数返回完整的REST API URL。
举个栗子:实战演练
假设你的WordPress站点URL是https://example.com
,并且启用了固定链接。
-
如果你调用
rest_url()
(或者rest_get_json_url()
),不传任何参数,那么它会返回https://example.com/wp-json/
。 -
如果你调用
rest_url( 'wp/v2/posts' )
,那么它会返回https://example.com/wp-json/wp/v2/posts
,这是获取所有文章的API地址。 -
如果你调用
rest_url( 'wp/v2/categories' )
,那么它会返回https://example.com/wp-json/wp/v2/categories
,这是获取所有分类的API地址。
参数详解:玩转URL
参数 | 类型 | 描述 | 默认值 |
---|---|---|---|
$path |
string | 可选参数,用于指定REST API的路径。例如,如果你想获取文章的API地址,你可以将 $path 设置为 'wp/v2/posts' 。如果你想获取特定ID的文章,你可以将 $path 设置为 'wp/v2/posts/123' (假设文章ID是123)。该参数将被添加到REST API的基础URL之后。注意:要确保路径以正斜杠 / 开头,函数内部会对路径进行处理,确保路径的正确性。 |
'/' |
$scheme |
string | 可选参数,用于指定URL的协议。默认情况下,它会使用当前页面的协议。你可以将其设置为 'http' 或 'https' ,以强制使用特定的协议。例如,如果你想确保API地址使用HTTPS协议,你可以将 $scheme 设置为 'https' 。 这在某些情况下非常有用,例如,当你的站点同时支持HTTP和HTTPS,并且你希望REST API始终使用HTTPS协议时。如果设置为 null ,则使用当前请求的协议。 |
null |
实际应用:如何获取文章和分类的API URL
现在,咱们来看看如何用rest_url()
(或者 rest_get_json_url()
) 获取文章和分类的API URL。
1. 获取所有文章的API URL:
$posts_url = rest_url( 'wp/v2/posts' );
echo $posts_url; // 输出:https://example.com/wp-json/wp/v2/posts
2. 获取指定ID的文章的API URL:
$post_id = 123; // 假设文章ID是123
$post_url = rest_url( 'wp/v2/posts/' . $post_id );
echo $post_url; // 输出:https://example.com/wp-json/wp/v2/posts/123
3. 获取所有分类的API URL:
$categories_url = rest_url( 'wp/v2/categories' );
echo $categories_url; // 输出:https://example.com/wp-json/wp/v2/categories
4. 获取指定ID的分类的API URL:
$category_id = 456; // 假设分类ID是456
$category_url = rest_url( 'wp/v2/categories/' . $category_id );
echo $category_url; // 输出:https://example.com/wp-json/wp/v2/categories/456
进阶技巧:使用过滤器定制URL
WordPress提供了一些过滤器,允许你定制REST API的URL。例如,你可以使用rest_url_prefix
过滤器来修改API的前缀,或者使用rest_url
过滤器来修改整个URL。
1. 修改API前缀:
add_filter( 'rest_url_prefix', 'my_custom_rest_prefix' );
function my_custom_rest_prefix( $prefix ) {
return 'api'; // 将前缀修改为'api'
}
// 之后,rest_url( 'wp/v2/posts' ) 将会返回:https://example.com/api/wp/v2/posts
2. 修改整个URL:
add_filter( 'rest_url', 'my_custom_rest_url', 10, 3 );
function my_custom_rest_url( $url, $path, $scheme ) {
// 这里可以根据需要修改$url
return $url . '?custom_param=value'; // 在URL后面添加一个自定义参数
}
// 之后,rest_url( 'wp/v2/posts' ) 将会返回:https://example.com/wp-json/wp/v2/posts?custom_param=value
注意事项:
- 版本问题: 虽然
rest_get_json_url()
已经被废弃,但为了兼容旧版本的WordPress,你可能仍然需要在代码中使用它。但是,建议你尽快切换到rest_url()
。 - 权限问题: 访问REST API需要相应的权限。例如,如果你想创建一个文章,你需要具有
edit_posts
权限。 - 安全问题: 在使用REST API时,要注意安全问题,例如防止跨站请求伪造(CSRF)攻击。
总结:
rest_get_json_url()
(或者 rest_url()
) 是一个非常方便的函数,可以帮助你轻松获取WordPress REST API的URL。通过理解它的工作原理和参数,你可以灵活地使用它来访问和操作WordPress的内容。同时,利用过滤器,你可以定制URL,以满足你的特定需求。
记住,从WordPress 5.5.0开始,你应该使用 rest_url()
函数代替 rest_get_json_url()
。
希望今天的讲解对你有所帮助!下次再见!