wppaste
WordPress

get_rest_url( int|null $blog_id = null, string $path = '/', string $scheme = 'rest' ): string

Since
4.4.0
Source
wp-includes/rest-api.php:519
Retrieves the URL to a REST endpoint on a site.

Description

Note: The returned URL is NOT escaped.

Compatibility

WordPress
since 4.4.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$blog_idint|nulloptional
Blog ID. Default of null returns URL for current blog.Default: null
$pathstringoptional
REST route. Default '/'.Default: '/'
$schemestringoptional
Sanitization scheme. Default 'rest'.Default: 'rest'

Return value

string
Full URL to the endpoint.

Performance profile

How much work a call to get_rest_url() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
46–86

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 112.

Plugin surface
1 hook

Third-party callbacks on 'rest_url' run inside this call, and their cost is not bounded by anything here.

Called by
8

8 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()one call below get_rest_url()
  • serializeserialisationmaybe_unserialize()one call below get_rest_url()

Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 45 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_rest_url() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_multisite() && get_option() && !is_admin()46–54ltrim(), is_multisite(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), is_admin(), apply_filters()
is_multisite() && get_blog_option() && !is_admin()47–55ltrim(), is_multisite(), get_blog_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), is_admin(), apply_filters()
!is_multisite() && get_option() && is_admin() && !force_ssl_admin()49–57ltrim(), is_multisite(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), is_admin(), force_ssl_admin(), apply_filters()
is_multisite() && get_blog_option() && is_admin() && !force_ssl_admin()50–58ltrim(), is_multisite(), get_blog_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), is_admin(), force_ssl_admin(), apply_filters()
is_multisite() && !get_blog_option() && get_option() && !is_admin()51–59ltrim(), is_multisite(), get_blog_option(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), is_admin(), apply_filters()
!is_multisite() && !get_option() && !is_admin()52–57ltrim(), is_multisite(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), is_admin(), apply_filters()
!is_multisite() && get_option() && is_admin() && force_ssl_admin()54–62ltrim(), is_multisite(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()
is_multisite() && !get_blog_option() && get_option() && is_admin() && !force_ssl_admin()54–62ltrim(), is_multisite(), get_blog_option(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), is_admin(), force_ssl_admin(), apply_filters()
!is_multisite() && !get_option() && is_admin() && !force_ssl_admin()55–60ltrim(), is_multisite(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), is_admin(), force_ssl_admin(), apply_filters()
is_multisite() && get_blog_option() && is_admin() && force_ssl_admin()55–63ltrim(), is_multisite(), get_blog_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()
is_multisite() && !get_blog_option() && !get_option() && !is_admin()57–62ltrim(), is_multisite(), get_blog_option(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), is_admin(), apply_filters()
is_multisite() && !get_blog_option() && get_option() && is_admin() && force_ssl_admin()59–67ltrim(), is_multisite(), get_blog_option(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()
33 further outcomes, up to 86 instructions
!is_multisite() && get_option() && is_ssl() && isset($value) && !is_admin()60–65ltrim(), is_multisite(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), is_admin(), apply_filters()
!is_multisite() && !get_option() && is_admin() && force_ssl_admin()60–65ltrim(), is_multisite(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()
is_multisite() && !get_blog_option() && !get_option() && is_admin() && !force_ssl_admin()60–65ltrim(), is_multisite(), get_blog_option(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), is_admin(), force_ssl_admin(), apply_filters()
is_multisite() && get_blog_option() && is_ssl() && isset($value) && !is_admin()61–66ltrim(), is_multisite(), get_blog_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), is_admin(), apply_filters()
!is_multisite() && get_option() && is_ssl() && isset($value) && is_admin() && !force_ssl_admin()63–68ltrim(), is_multisite(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), is_admin(), force_ssl_admin(), apply_filters()
is_multisite() && get_blog_option() && is_ssl() && isset($value) && is_admin() && !force_ssl_admin()64–69ltrim(), is_multisite(), get_blog_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), is_admin(), force_ssl_admin(), apply_filters()
!is_multisite() && get_option() && is_ssl() && isset($value) && !is_admin()65–70ltrim(), is_multisite(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), apply_filters()
is_multisite() && !get_blog_option() && get_option() && is_ssl() && isset($value) && !is_admin()65–70ltrim(), is_multisite(), get_blog_option(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), is_admin(), apply_filters()
is_multisite() && !get_blog_option() && !get_option() && is_admin() && force_ssl_admin()65–70ltrim(), is_multisite(), get_blog_option(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()
!is_multisite() && !get_option() && is_ssl() && isset($value) && !is_admin()66–68ltrim(), is_multisite(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), get_home_url(), parse_url(), is_admin(), apply_filters()
is_multisite() && get_blog_option() && is_ssl() && isset($value) && !is_admin()66–71ltrim(), is_multisite(), get_blog_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), apply_filters()
!is_multisite() && get_option() && is_ssl() && isset($value) && is_admin() && force_ssl_admin()68–73ltrim(), is_multisite(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()
!is_multisite() && get_option() && is_ssl() && isset($value) && is_admin() && !force_ssl_admin()68–73ltrim(), is_multisite(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), force_ssl_admin(), apply_filters()
is_multisite() && !get_blog_option() && get_option() && is_ssl() && isset($value) && is_admin() && !force_ssl_admin()68–73ltrim(), is_multisite(), get_blog_option(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), is_admin(), force_ssl_admin(), apply_filters()
!is_multisite() && !get_option() && is_ssl() && isset($value) && is_admin() && !force_ssl_admin()69–71ltrim(), is_multisite(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), get_home_url(), parse_url(), is_admin(), force_ssl_admin(), apply_filters()
is_multisite() && get_blog_option() && is_ssl() && isset($value) && is_admin() && force_ssl_admin()69–74ltrim(), is_multisite(), get_blog_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()
is_multisite() && get_blog_option() && is_ssl() && isset($value) && is_admin() && !force_ssl_admin()69–74ltrim(), is_multisite(), get_blog_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), force_ssl_admin(), apply_filters()
is_multisite() && !get_blog_option() && get_option() && is_ssl() && isset($value) && !is_admin()70–75ltrim(), is_multisite(), get_blog_option(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), apply_filters()
!is_multisite() && !get_option() && is_ssl() && isset($value) && !is_admin()71–73ltrim(), is_multisite(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), apply_filters()
is_multisite() && !get_blog_option() && !get_option() && is_ssl() && isset($value) && !is_admin()71–73ltrim(), is_multisite(), get_blog_option(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), get_home_url(), parse_url(), is_admin(), apply_filters()
!is_multisite() && get_option() && is_ssl() && isset($value) && is_admin() && force_ssl_admin()73–78ltrim(), is_multisite(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()
is_multisite() && !get_blog_option() && get_option() && is_ssl() && isset($value) && is_admin() && force_ssl_admin()73–78ltrim(), is_multisite(), get_blog_option(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()
is_multisite() && !get_blog_option() && get_option() && is_ssl() && isset($value) && is_admin() && !force_ssl_admin()73–78ltrim(), is_multisite(), get_blog_option(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), force_ssl_admin(), apply_filters()
!is_multisite() && !get_option() && is_ssl() && isset($value) && is_admin() && force_ssl_admin()74–76ltrim(), is_multisite(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), get_home_url(), parse_url(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()
!is_multisite() && !get_option() && is_ssl() && isset($value) && is_admin() && !force_ssl_admin()74–76ltrim(), is_multisite(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), force_ssl_admin(), apply_filters()
is_multisite() && get_blog_option() && is_ssl() && isset($value) && is_admin() && force_ssl_admin()74–79ltrim(), is_multisite(), get_blog_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()
is_multisite() && !get_blog_option() && !get_option() && is_ssl() && isset($value) && is_admin() && !force_ssl_admin()74–76ltrim(), is_multisite(), get_blog_option(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), get_home_url(), parse_url(), is_admin(), force_ssl_admin(), apply_filters()
is_multisite() && !get_blog_option() && !get_option() && is_ssl() && isset($value) && !is_admin()76–78ltrim(), is_multisite(), get_blog_option(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), apply_filters()
is_multisite() && !get_blog_option() && get_option() && is_ssl() && isset($value) && is_admin() && force_ssl_admin()78–83ltrim(), is_multisite(), get_blog_option(), get_option(), ->using_index_permalinks(), rest_get_url_prefix(), get_home_url(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()
!is_multisite() && !get_option() && is_ssl() && isset($value) && is_admin() && force_ssl_admin()79–81ltrim(), is_multisite(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()
is_multisite() && !get_blog_option() && !get_option() && is_ssl() && isset($value) && is_admin() && force_ssl_admin()79–81ltrim(), is_multisite(), get_blog_option(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), get_home_url(), parse_url(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()
is_multisite() && !get_blog_option() && !get_option() && is_ssl() && isset($value) && is_admin() && !force_ssl_admin()79–81ltrim(), is_multisite(), get_blog_option(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), force_ssl_admin(), apply_filters()
is_multisite() && !get_blog_option() && !get_option() && is_ssl() && isset($value) && is_admin() && force_ssl_admin()84–86ltrim(), is_multisite(), get_blog_option(), get_option(), get_home_url(), trailingslashit(), str_ends_with(), add_query_arg(), is_ssl(), get_home_url(), parse_url(), set_url_scheme(), is_admin(), force_ssl_admin(), set_url_scheme(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 112 instructions, 46–86 executed per call, 11 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while get_rest_url() runs, in this order:

  1. apply_filters( rest_url )filterline 577 (+58 into the body)

    Filters the REST URL.

Uses · 13

Show all 13
  • apply_filters()Calls the callback functions that have been added to a filter hook.

Used by · 8

Source code

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 ( ! str_ends_with( $url, 'index.php' ) ) {			$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 );}

Changelog

Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/rest-api.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.