WP_REST_Request::from_url( string $url ): WP_REST_Request|false
- Since
- 4.5.0
- Source
wp-includes/rest-api/class-wp-rest-request.php:1044
Compatibility
- WordPress
- since 4.5.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
$urlstring- URL with protocol, domain, path and query args.
Return value
WP_REST_Request|false- WP_REST_Request object on success, false on failure.
Performance profile
How much work a call to WP_REST_Request::from_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
- 26–51
- Plugin surface
- 1 hook
- Called by
- 2
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 55.
Third-party callbacks on 'rest_request_from_url' run inside this call, and their cost is not bounded by anything here.
2 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 WP_REST_Request::from_url() - serializeserialisation
maybe_unserialize()one call below WP_REST_Request::from_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 · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Request::from_url() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($bits) && empty($route) | 26–30 | parse_url(), rest_url(), get_option(), apply_filters() |
!empty($bits) && empty($route) | 32–36 | parse_url(), wp_parse_str(), rest_url(), get_option(), apply_filters() |
empty($bits) && !empty($route) | 34–38 | parse_url(), rest_url(), get_option(), ->set_query_params(), apply_filters() |
empty($bits) && get_option() && $url && empty($route) | 37 | parse_url(), rest_url(), get_option(), untrailingslashit(), parse_url(), apply_filters() |
!empty($bits) && !empty($route) | 40–44 | parse_url(), wp_parse_str(), rest_url(), get_option(), ->set_query_params(), apply_filters() |
!empty($bits) && get_option() && $url && empty($route) | 43 | parse_url(), wp_parse_str(), rest_url(), get_option(), untrailingslashit(), parse_url(), apply_filters() |
empty($bits) && get_option() && $url && !empty($route) | 45 | parse_url(), rest_url(), get_option(), untrailingslashit(), parse_url(), ->set_query_params(), apply_filters() |
!empty($bits) && get_option() && $url && !empty($route) | 51 | parse_url(), wp_parse_str(), rest_url(), get_option(), untrailingslashit(), parse_url(), ->set_query_params(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 55 | 26–51 | 5 | |
| 8.5 | 55 | 26–51 | 5 | |
| 8.4 | 55 | 26–51 | 5 | 7 fewer instructions than PHP 8.3 |
| 8.3 | 62 | 26–58 | 5 | |
| 8.2 | 62 | 26–58 | 5 | |
| 8.1 | 62 | 26–58 | 5 | |
| 7.4 | 62 | 26–58 | 5 |
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 WP_REST_Request::from_url() runs, in this order:
- apply_filters( rest_request_from_url )filterline 1078 (+34 into the body)
Filters the REST API request generated from a URL.
Uses · 7
- wp_parse_str()Parses a string into variables to be stored in an array.
- rest_url()Retrieves the URL to a REST endpoint.
- get_option()Retrieves an option value based on an option name.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- untrailingslashit()Removes trailing forward slashes and backslashes if they exist.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_REST_Request::__construct()Constructor.
Used by · 2
- WP_REST_Server::embed_links()Embeds the links from the data into the request.
- WP_REST_Server::get_target_hints_for_link()Gets the target links for a REST API Link.
Source code
public static function from_url( $url ) { $bits = parse_url( $url ); $query_params = array(); if ( ! empty( $bits['query'] ) ) { wp_parse_str( $bits['query'], $query_params ); } $api_root = rest_url(); if ( get_option( 'permalink_structure' ) && str_starts_with( $url, $api_root ) ) { // Pretty permalinks on, and URL is under the API root. $api_url_part = substr( $url, strlen( untrailingslashit( $api_root ) ) ); $route = parse_url( $api_url_part, PHP_URL_PATH ); } elseif ( ! empty( $query_params['rest_route'] ) ) { // ?rest_route=... set directly. $route = $query_params['rest_route']; unset( $query_params['rest_route'] ); } $request = false; if ( ! empty( $route ) ) { $request = new WP_REST_Request( 'GET', $route ); $request->set_query_params( $query_params ); } /** * Filters the REST API request generated from a URL. * * @since 4.5.0 * * @param WP_REST_Request|false $request Generated request object, or false if URL * could not be parsed. * @param string $url URL the request was generated from. */ return apply_filters( 'rest_request_from_url', $request, $url ); }Changelog
Introduced in 4.5.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.8.8 tag, from
src/wp-includes/rest-api/class-wp-rest-request.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.