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 );
}
Related
| Uses | Description |
|---|---|
get_rest_url()wp-includes/rest-api.php |
Retrieves the URL to a REST endpoint on a site. |
| Used by | Description |
|---|---|
WP_REST_Attachments_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php |
Prepares attachment links for the request. |
WP_REST_Abilities_V1_Categories_Controller::get_items()wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-categories-controller.php |
Retrieves all ability categories. |
WP_REST_Abilities_V1_Categories_Controller::prepare_item_for_response()wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-categories-controller.php |
Prepares an ability category for response. |
WP_REST_Abilities_V1_List_Controller::get_items()wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-list-controller.php |
Retrieves all abilities. |
WP_REST_Abilities_V1_List_Controller::prepare_item_for_response()wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-list-controller.php |
Prepares an ability for response. |
WP_REST_Font_Faces_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php |
Prepares links for the request. |
WP_REST_Font_Families_Controller::prepare_font_face_links()wp-includes/rest-api/endpoints/class-wp-rest-font-families-controller.php |
Prepares child font face links for the request. |
WP_REST_Font_Collections_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php |
Prepares links for the request. |
WP_REST_Font_Collections_Controller::get_items()wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php |
Gets the font collections available. |
WP_REST_Template_Autosaves_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-template-autosaves-controller.php |
Prepares links for the request. |
WP_REST_Template_Revisions_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-template-revisions-controller.php |
Prepares links for the request. |
WP_REST_Navigation_Fallback_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-navigation-fallback-controller.php |
Prepares the links for the request. |
WP_REST_Global_Styles_Revisions_Controller::get_items()wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php |
Returns paginated revisions of the given global styles config custom post type. |
WP_REST_Post_Types_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php |
Prepares links for the request. |
WP_REST_Taxonomies_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php |
Prepares links for the request. |
WP_REST_Server::add_image_to_index()wp-includes/rest-api/class-wp-rest-server.php |
Exposes an image through the WordPress REST API. |
WP_REST_Menu_Items_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php |
Prepares links for the request. |
WP_REST_Menu_Items_Controller::get_schema_links()wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php |
Retrieves Link Description Objects that should be added to the Schema for the nav menu items collection. |
WP_REST_Global_Styles_Controller::get_theme_item()wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php |
Returns the given theme global styles config. |
WP_REST_Menu_Items_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php |
Creates a single nav menu item. |
WP_REST_Global_Styles_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php |
Prepares links for the request. |
WP_REST_Menus_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php |
Prepares links for the request. |
WP_REST_Menus_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-menus-controller.php |
Creates a single term in a taxonomy. |
WP_REST_Menu_Locations_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php |
Prepares links for the request. |
WP_REST_Widgets_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php |
Prepares links for the widget. |
WP_REST_Sidebars_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php |
Prepares links for the sidebar. |
WP_REST_Templates_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php |
Prepares links for the request. |
WP_REST_Templates_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php |
Creates a single template. |
WP_REST_Widget_Types_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-widget-types-controller.php |
Prepares links for the widget type. |
WP_REST_Server::add_active_theme_link_to_index()wp-includes/rest-api/class-wp-rest-server.php |
Adds a link to the active theme for users who have proper permissions. |
WP_REST_Themes_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php |
Prepares links for the request. |
WP_REST_Application_Passwords_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php |
Prepares links for the request. |
WP_REST_Term_Search_Handler::prepare_item_links()wp-includes/rest-api/search/class-wp-rest-term-search-handler.php |
Prepares links for the search result of a given ID. |
WP_REST_Block_Directory_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php |
Generates a list of links to include in the response for the plugin. |
WP_REST_Plugins_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php |
Prepares links for the request. |
WP_REST_Plugins_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php |
Uploads a plugin and optionally activates it. |
WP_REST_Block_Types_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php |
Prepares links for the request. |
WP_REST_Attachments_Controller::edit_media_item()wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php |
Applies edits to a media item and creates a new attachment record. |
WP_Site_Health::get_test_rest_availability()wp-admin/includes/class-wp-site-health.php |
Tests if the REST API is accessible. |
WP_Site_Health::get_tests()wp-admin/includes/class-wp-site-health.php |
Returns a set of tests that belong to the site status page. |
WP_REST_Search_Controller::get_items()wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php |
Retrieves a collection of search results. |
WP_REST_Search_Controller::prepare_item_for_response()wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php |
Prepares a single search result for response. |
WP_REST_Post_Search_Handler::prepare_item_links()wp-includes/rest-api/search/class-wp-rest-post-search-handler.php |
Prepares links for the search result of a given ID. |
WP_REST_Posts_Controller::get_schema_links()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Retrieves Link Description Objects that should be added to the Schema for the posts collection. |
WP_REST_Users_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php |
Prepares links for the user request. |
WP_REST_Users_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php |
Creates a single user. |
WP_REST_Users_Controller::get_items()wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php |
Retrieves all users. |
WP_REST_Revisions_Controller::prepare_item_for_response()wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php |
Prepares the revision for the REST response. |
WP_REST_Revisions_Controller::get_items()wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php |
Gets a collection of revisions. |
WP_REST_Attachments_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php |
Creates a single attachment. |
WP_REST_Post_Statuses_Controller::prepare_item_for_response()wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php |
Prepares a post status object for serialization. |
WP_REST_Terms_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php |
Prepares links for the request. |
WP_REST_Terms_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php |
Creates a single term in a taxonomy. |
WP_REST_Terms_Controller::get_items()wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php |
Retrieves terms associated with a taxonomy. |
WP_REST_Posts_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Prepares links for the request. |
WP_REST_Posts_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Creates a single post. |
WP_REST_Posts_Controller::get_items()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php |
Retrieves a collection of posts. |
WP_REST_Comments_Controller::prepare_links()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php |
Prepares links for the request. |
WP_REST_Comments_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php |
Creates a comment. |
WP_REST_Comments_Controller::get_items()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php |
Retrieves a list of comment items. |
WP_REST_Request::from_url()wp-includes/rest-api/class-wp-rest-request.php |
Retrieves a WP_REST_Request object from a full URL. |
rest_output_link_wp_head()wp-includes/rest-api.php |
Outputs the REST API link tag into page header. |
rest_output_link_header()wp-includes/rest-api.php |
Sends a Link header for the REST API. |
get_oembed_endpoint_url()wp-includes/embed.php |
Retrieves the oEmbed endpoint URL for a given permalink. |
WP_REST_Server::get_data_for_route()wp-includes/rest-api/class-wp-rest-server.php |
Retrieves publicly-visible data for the route. |
WP_REST_Server::get_namespace_index()wp-includes/rest-api/class-wp-rest-server.php |
Retrieves the index for a namespace. |
wp_enqueue_media()wp-includes/media.php |
Enqueues all scripts, styles, settings, and templates necessary to use all media JS APIs. |
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Introduced. |
Skip to note 2 content
jakeparis
As an example, to get the rest endpoint for a given post:
$post_id = 1234 $endpoint = rest_url("wp/v2/posts/{$post_id}/“);