On this page
get_rest_url( int|null $blog_id = null, string $path = '/', string $scheme = 'rest' ): string
Retrieves the URL to a REST endpoint on a site.
Description
Note: The returned URL is NOT escaped.
Parameters
$blog_idint|null Optional-
Blog ID. Default of null returns URL for current blog.
Default:
null $pathstring Optional-
REST route. Default
'/'.Default:
'/' $schemestring Optional-
Sanitization scheme. Default
'rest'.Default:
'rest'
Return
string Full URL to the endpoint.
Source
File: wp-includes/rest-api.php. View all references
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 );
}
Hooks
Related
Uses
| Uses | Description |
|---|---|
| rest_get_url_prefix() wp-includes/rest-api.php | Retrieves the URL prefix for any API resource. |
| is_ssl() wp-includes/load.php | Determines if SSL is used. |
| force_ssl_admin() wp-includes/functions.php | Determines whether to force SSL used for the Administration Screens. |
| set_url_scheme() wp-includes/link-template.php | Sets the scheme for a URL. |
| get_home_url() wp-includes/link-template.php | Retrieves the URL for a given site where the front end is accessible. |
| WP_Rewrite::using_index_permalinks() wp-includes/class-wp-rewrite.php | Determines whether permalinks are being used and rewrite module is not enabled. |
| get_blog_option() wp-includes/ms-blogs.php | Retrieve option value for a given blog id based on name of option. |
| trailingslashit() wp-includes/formatting.php | Appends a trailing slash. |
| is_multisite() wp-includes/load.php | If Multisite is enabled. |
| is_admin() wp-includes/load.php | Determines whether the current request is for an administrative interface page. |
| add_query_arg() wp-includes/functions.php | Retrieves a modified URL query string. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| get_option() wp-includes/option.php | Retrieves an option value based on an option name. |
Used By
| Used By | Description |
|---|---|
| wp_is_local_html_output() wp-includes/https-detection.php | Checks whether a given HTML string is likely an output from this WordPress site. |
| wp_default_packages_inline_scripts() wp-includes/script-loader.php | Adds inline scripts required for the WordPress JavaScript packages. |
| rest_output_rsd() wp-includes/rest-api.php | Adds the REST API URL to the WP RSD endpoint. |
| 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. |
| rest_url() wp-includes/rest-api.php | Retrieves the URL to a REST endpoint. |
| WP_REST_Server::serve_request() wp-includes/rest-api/class-wp-rest-server.php | Handles serving a REST API request. |
| wp_default_scripts() wp-includes/script-loader.php | Registers all WordPress scripts. |
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_rest_url