函数文档

rest_url()

💡 云策文档标注

概述

rest_url() 函数用于获取 WordPress REST API 端点的完整 URL。它基于 get_rest_url() 实现,返回的 URL 未经过转义处理,开发者需注意安全使用。

关键要点

  • 功能:返回指定 REST 路由的完整 URL,适用于构建 REST API 请求链接。
  • 参数:$path(可选,REST 路由,默认为空字符串),$scheme(可选,清理方案,默认为 'rest')。
  • 返回值:字符串类型的端点完整 URL。
  • 注意事项:返回的 URL 未转义,使用时需根据上下文进行适当转义以防止安全风险。

代码示例

$post_id = 1234;
$endpoint = rest_url("wp/v2/posts/{$post_id}/");

📄 原文内容

Retrieves the URL to a REST endpoint.

Description

Note: The returned URL is NOT escaped.

Parameters

$pathstringoptional
REST route. Default empty.
$schemestringoptional
Sanitization scheme. Default 'rest'.

Return

string Full URL to the endpoint.

Source

function rest_url( $path = '', $scheme = 'rest' ) {
	return get_rest_url( null, $path, $scheme );
}

Changelog

Version Description
4.4.0 Introduced.

User Contributed Notes