WP::parse_request( array|string $extra_query_vars = '' ): bool
- Since
- 2.0.0, 6.0.0
- Source
wp-includes/class-wp.php:136
Description
Sets up the query variables based on the request. There are also many filters and actions that can be used to further manipulate the result.
Compatibility
- WordPress
- since 6.0.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
$extra_query_varsarray|stringoptional- Set the extra query variables.Default:
''
Return value
bool- Whether the request was parsed.
Performance profile
How much work a call to WP::parse_request() 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
- Heavy
- Scaling
- Scales with input
- Instructions
- 79–182
- Plugin surface
- 4 hooks
- Called by
- 1
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 486.
Third-party callbacks on 'do_parse_request', 'query_vars', 'request' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get_last_changed()one call below WP::parse_request() - querycontent query
get_post()one call below WP::parse_request() - optionoption read or write
get_option()one call below WP::parse_request()
Further down the call graph this can also reach serialize 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 · 12 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP::parse_request() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
apply_filters() && !is_array($extra_query_vars) && empty($extra_query_vars) && empty($rewrite) && is_admin() && !isset($value) | 79–86 | apply_filters(), ->wp_rewrite_rules(), apply_filters(), get_post_types(), get_taxonomies(), is_admin(), wp_resolve_numeric_slug_conflicts(), apply_filters() |
apply_filters() && !is_array($extra_query_vars) && empty($extra_query_vars) && empty($rewrite) && !is_admin() && !isset($value) | 85–93 | apply_filters(), ->wp_rewrite_rules(), apply_filters(), get_post_types(), get_taxonomies(), is_admin(), get_taxonomies(), wp_resolve_numeric_slug_conflicts(), apply_filters() |
apply_filters() && !is_array($extra_query_vars) && empty($extra_query_vars) && empty($rewrite) && is_admin() && isset($value) | 92–102 | apply_filters(), ->wp_rewrite_rules(), apply_filters(), get_post_types(), get_taxonomies(), is_admin(), get_post_types(), wp_resolve_numeric_slug_conflicts(), apply_filters() |
apply_filters() && !is_array($extra_query_vars) && empty($extra_query_vars) && empty($rewrite) && is_admin() && isset($value) | 96–103 | apply_filters(), ->wp_rewrite_rules(), apply_filters(), get_post_types(), get_taxonomies(), is_admin(), get_post_types(), array_intersect(), wp_resolve_numeric_slug_conflicts(), apply_filters() |
apply_filters() && !is_array($extra_query_vars) && empty($extra_query_vars) && empty($rewrite) && !is_admin() && isset($value) | 98–109 | apply_filters(), ->wp_rewrite_rules(), apply_filters(), get_post_types(), get_taxonomies(), is_admin(), get_taxonomies(), get_post_types(), wp_resolve_numeric_slug_conflicts(), apply_filters() |
apply_filters() && !is_array($extra_query_vars) && empty($extra_query_vars) && empty($rewrite) && !is_admin() && isset($value) | 102–110 | apply_filters(), ->wp_rewrite_rules(), apply_filters(), get_post_types(), get_taxonomies(), is_admin(), get_taxonomies(), get_post_types(), array_intersect(), wp_resolve_numeric_slug_conflicts(), apply_filters() |
apply_filters() && !is_array($extra_query_vars) && empty($extra_query_vars) && !empty($rewrite) && !is_string($home_path) && empty($home_path_regex) && empty($pathinfo) && $req_uri !== false && !empty($request_match) && $rewrite && empty($value) && empty($requested_path) && !isset($perma_query_vars) && get_post_types() && is_admin() && !isset($value) | 153–158 | apply_filters(), ->wp_rewrite_rules(), explode(), explode(), home_url(), parse_url(), apply_filters(), get_post_types(), get_taxonomies(), is_admin(), wp_resolve_numeric_slug_conflicts(), apply_filters() |
apply_filters() && !is_array($extra_query_vars) && empty($extra_query_vars) && !empty($rewrite) && !is_string($home_path) && empty($home_path_regex) && empty($pathinfo) && $req_uri !== false && !empty($request_match) && $rewrite && empty($value) && empty($requested_path) && !isset($perma_query_vars) && get_post_types() && !is_admin() && !isset($value) | 159–165 | apply_filters(), ->wp_rewrite_rules(), explode(), explode(), home_url(), parse_url(), apply_filters(), get_post_types(), get_taxonomies(), is_admin(), get_taxonomies(), wp_resolve_numeric_slug_conflicts(), apply_filters() |
apply_filters() && !is_array($extra_query_vars) && empty($extra_query_vars) && !empty($rewrite) && !is_string($home_path) && empty($home_path_regex) && empty($pathinfo) && $req_uri !== false && !empty($request_match) && $rewrite && empty($value) && empty($requested_path) && !isset($perma_query_vars) && get_post_types() && is_admin() && isset($value) | 166–174 | apply_filters(), ->wp_rewrite_rules(), explode(), explode(), home_url(), parse_url(), apply_filters(), get_post_types(), get_taxonomies(), is_admin(), get_post_types(), wp_resolve_numeric_slug_conflicts(), apply_filters() |
apply_filters() && !is_array($extra_query_vars) && empty($extra_query_vars) && !empty($rewrite) && !is_string($home_path) && empty($home_path_regex) && empty($pathinfo) && $req_uri !== false && !empty($request_match) && $rewrite && empty($value) && empty($requested_path) && !isset($perma_query_vars) && get_post_types() && is_admin() && isset($value) | 170–175 | apply_filters(), ->wp_rewrite_rules(), explode(), explode(), home_url(), parse_url(), apply_filters(), get_post_types(), get_taxonomies(), is_admin(), get_post_types(), array_intersect(), wp_resolve_numeric_slug_conflicts(), apply_filters() |
apply_filters() && !is_array($extra_query_vars) && empty($extra_query_vars) && !empty($rewrite) && !is_string($home_path) && empty($home_path_regex) && empty($pathinfo) && $req_uri !== false && !empty($request_match) && $rewrite && empty($value) && empty($requested_path) && !isset($perma_query_vars) && get_post_types() && !is_admin() && isset($value) | 172–181 | apply_filters(), ->wp_rewrite_rules(), explode(), explode(), home_url(), parse_url(), apply_filters(), get_post_types(), get_taxonomies(), is_admin(), get_taxonomies(), get_post_types(), wp_resolve_numeric_slug_conflicts(), apply_filters() |
apply_filters() && !is_array($extra_query_vars) && empty($extra_query_vars) && !empty($rewrite) && !is_string($home_path) && empty($home_path_regex) && empty($pathinfo) && $req_uri !== false && !empty($request_match) && $rewrite && empty($value) && empty($requested_path) && !isset($perma_query_vars) && get_post_types() && !is_admin() && isset($value) | 176–182 | apply_filters(), ->wp_rewrite_rules(), explode(), explode(), home_url(), parse_url(), apply_filters(), get_post_types(), get_taxonomies(), is_admin(), get_taxonomies(), get_post_types(), array_intersect(), wp_resolve_numeric_slug_conflicts(), apply_filters() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 486 | 79–182 | 68 | |
| 8.5 | 486 | 79–182 | 68 | |
| 8.4 | 486 | 79–182 | 68 | 58 fewer instructions than PHP 8.3 |
| 8.3 | 544 | 79–199 | 68 | |
| 8.2 | 544 | 79–199 | 68 | |
| 8.1 | 544 | 79–199 | 68 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 548 | 79–199 | 68 |
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 · 4
4 hooks fire while WP::parse_request() runs, in this order:
- apply_filters( do_parse_request )filterline 148 (+12 into the body)
Filters whether to parse the request.
- apply_filters( query_vars )filterline 311 (+175 into the body)
Filters the query variables allowed before processing.
- apply_filters( request )filterline 409 (+273 into the body)
Filters the array of parsed query variables.
- do_action( parse_request )actionline 418 (+282 into the body)
Fires once all query variables for the current request have been parsed.
Uses · 15
- apply_filters()Calls the callback functions that have been added to a filter hook.
- home_url()Retrieves the URL for the current site where the front end is accessible.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- get_page_by_path()Retrieves a page given its path.
- get_post_status_object()Retrieves a post status object by name.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- get_post_types()Gets a list of all registered post type objects.
- is_post_type_viewable()Determines whether a post type is considered "viewable".
- wp_die()Kills WordPress execution and displays HTML page with an error message.
- __()Retrieves the translation of $text.
- get_taxonomies()Retrieves a list of registered taxonomy names or objects.
- is_admin()Determines whether the current request is for an administrative interface page.
Show all 15
- wp_resolve_numeric_slug_conflicts()Resolves numeric slugs that collide with date permalinks.
- do_action_ref_array()Calls the callback functions that have been added to an action hook, specifying arguments in an array.
- WP_MatchesMapRegex::apply()Substitute substring matches in subject.
Used by · 1
- WP::main()Sets up all of the variables required by the WordPress environment.
Source code
public function parse_request( $extra_query_vars = '' ) { global $wp_rewrite; /** * Filters whether to parse the request. * * @since 3.5.0 * * @param bool $bool Whether or not to parse the request. Default true. * @param WP $wp Current WordPress environment instance. * @param array|string $extra_query_vars Extra passed query variables. */ if ( ! apply_filters( 'do_parse_request', true, $this, $extra_query_vars ) ) { return false; } $this->query_vars = array(); $post_type_query_vars = array(); if ( is_array( $extra_query_vars ) ) { $this->extra_query_vars = & $extra_query_vars; } elseif ( ! empty( $extra_query_vars ) ) { parse_str( $extra_query_vars, $this->extra_query_vars ); } // Process PATH_INFO, REQUEST_URI, and 404 for permalinks. // Fetch the rewrite rules. $rewrite = $wp_rewrite->wp_rewrite_rules(); if ( ! empty( $rewrite ) ) { // If we match a rewrite rule, this will be cleared. $error = '404'; $this->did_permalink = true; $pathinfo = $_SERVER['PATH_INFO'] ?? ''; list( $pathinfo ) = explode( '?', $pathinfo ); $pathinfo = str_replace( '%', '%25', $pathinfo ); list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] ); $self = $_SERVER['PHP_SELF']; $home_path = parse_url( home_url(), PHP_URL_PATH ); $home_path_regex = ''; if ( is_string( $home_path ) && '' !== $home_path ) { $home_path = trim( $home_path, '/' ); $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) ); } /* * Trim path info from the end and the leading home path from the front. * For path info requests, this leaves us with the requesting filename, if any. * For 404 requests, this leaves us with the requested permalink. */ $req_uri = str_replace( $pathinfo, '', $req_uri ); $req_uri = trim( $req_uri, '/' ); $pathinfo = trim( $pathinfo, '/' ); $self = trim( $self, '/' ); if ( ! empty( $home_path_regex ) ) { $req_uri = preg_replace( $home_path_regex, '', $req_uri ); $req_uri = trim( $req_uri, '/' ); $pathinfo = preg_replace( $home_path_regex, '', $pathinfo ); $pathinfo = trim( $pathinfo, '/' ); $self = preg_replace( $home_path_regex, '', $self ); $self = trim( $self, '/' ); } // The requested permalink is in $pathinfo for path info requests and $req_uri for other requests. if ( ! empty( $pathinfo ) && ! preg_match( '|^.*' . $wp_rewrite->index . '$|', $pathinfo ) ) { $requested_path = $pathinfo; } else { // If the request uri is the index, blank it out so that we don't try to match it against a rule. if ( $req_uri === $wp_rewrite->index ) { $req_uri = ''; } $requested_path = $req_uri; } $requested_file = $req_uri;Changelog
Introduced in 2.0.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 7.1.0 tag, from
src/wp-includes/class-wp.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.