get_rest_url
Retrieves the URL to a REST endpoint on a site.
Description
(string) get_rest_url( (constant) $blog_id = null, (string) $path = '/', (string) $scheme = 'rest' );
Note: The returned URL is NOT escaped.
Returns (string)
Full URL to the endpoint.
Parameters (3)
- 0. $blog_id — Optional. (constant) =>
null
- Blog ID. Default of null returns URL for current blog.
- 1. $path — Optional. (string) =>
'/'
- REST route. Default /..
- 2. $scheme — Optional. (string) =>
'rest'
- Sanitization scheme. Default rest..
Usage
if ( !function_exists( 'get_rest_url' ) ) { require_once ABSPATH . WPINC . '/rest-api.php'; } // Optional. Blog ID. Default of null returns URL for current blog. $blog_id = null; // Optional. REST route. Default '/'. $path = '/'; // Optional. Sanitization scheme. Default 'rest'. $scheme = 'rest'; // NOTICE! Understand what this does before running. $result = get_rest_url($blog_id, $path, $scheme);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/rest-api.php
- function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) {
- if ( empty( $path ) ) {
- $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 .= '/' . ltrim( $path, '/' );
- } else {
- $url = trailingslashit( get_home_url( $blog_id, '', $scheme ) );
- $path = '/' . ltrim( $path, '/' );
- $url = add_query_arg( 'rest_route', $path, $url );
- }
- if ( is_ssl() ) {
- // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
- if ( $_SERVER['SERVER_NAME'] === parse_url( get_home_url( $blog_id ), PHP_URL_HOST ) ) {
- $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 $blog_id Blog ID.
- * @param string $scheme Sanitization scheme.
- */
- return apply_filters( 'rest_url', $url, $path, $blog_id, $scheme );
- }