get_rest_url( int|null $blog_id = null, string $path = '/', string $scheme = 'rest' ): string
- Since
- 4.4.0
- Source
wp-includes/rest-api.php:515
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
- Scaling
- Constant
- Instructions
- 46–86
- Plugin surface
- 1 hook
- Called by
- 8
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 112.
Third-party callbacks on 'rest_url' run inside this call, and their cost is not bounded by anything here.
8 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()one call below get_rest_url() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
!is_multisite() && get_option() && !is_admin() | 46–54 | ltrim(), 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–55 | ltrim(), 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–57 | ltrim(), 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–58 | ltrim(), 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–59 | ltrim(), 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–57 | ltrim(), 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–62 | ltrim(), 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–62 | ltrim(), 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–60 | ltrim(), 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–63 | ltrim(), 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–62 | ltrim(), 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–67 | ltrim(), 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–65 | ltrim(), 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–65 | ltrim(), 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–65 | ltrim(), 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–66 | ltrim(), 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–68 | ltrim(), 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–69 | ltrim(), 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–70 | ltrim(), 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–70 | ltrim(), 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–70 | ltrim(), 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–68 | ltrim(), 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–71 | ltrim(), 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–73 | ltrim(), 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–73 | ltrim(), 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–73 | ltrim(), 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–71 | ltrim(), 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–74 | ltrim(), 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–74 | ltrim(), 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–75 | ltrim(), 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–73 | ltrim(), 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–73 | ltrim(), 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–78 | ltrim(), 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–78 | ltrim(), 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–78 | ltrim(), 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–76 | ltrim(), 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–76 | ltrim(), 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–79 | ltrim(), 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–76 | ltrim(), 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–78 | ltrim(), 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–83 | ltrim(), 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–81 | ltrim(), 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–81 | ltrim(), 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–81 | ltrim(), 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–86 | ltrim(), 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:
Uses · 13
- is_multisite()Determines whether Multisite is enabled.
- get_blog_option()Retrieves option value for a given blog id based on name of option.
- get_option()Retrieves an option value based on an option name.
- get_home_url()Retrieves the URL for a given site where the front end is accessible.
- rest_get_url_prefix()Retrieves the URL prefix for any API resource.
- trailingslashit()Appends a trailing slash.
- str_ends_with()Polyfill for `str_ends_with()` function added in PHP 8.0.
- add_query_arg()Retrieves a modified URL query string.
- is_ssl()Determines if SSL is used.
- set_url_scheme()Sets the scheme for a URL.
- is_admin()Determines whether the current request is for an administrative interface page.
- force_ssl_admin()Determines whether to force SSL used for the Administration Screens.
Show all 13
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 8
- WP_REST_Server::serve_request()Handles serving a REST API request.
- rest_output_link_header()Sends a Link header for the REST API.
- rest_output_link_wp_head()Outputs the REST API link tag into page header.
- rest_output_rsd()Adds the REST API URL to the WP RSD endpoint.
- rest_url()Retrieves the URL to a REST endpoint.
- wp_default_packages_inline_scripts()Adds inline scripts required for the WordPress JavaScript packages.
- wp_default_scripts()Registers all WordPress scripts.
- wp_is_local_html_output()Checks whether a given HTML string is likely an output from this WordPress site.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.