wppaste
WordPress

WP::parse_request( array|string $extra_query_vars = '' ): bool

Since
2.0.0, 6.0.0
Source
wp-includes/class-wp.php:136
Parses the request to find the correct WordPress query.

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

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
79–182

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

Plugin surface
4 hooks

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

Called by
1

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

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get_last_changed()one call below WP::parse_request()
  • querycontent queryget_post()one call below WP::parse_request()
  • optionoption read or writeget_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.

WhenInstructionsCalls it makes
apply_filters() && !is_array($extra_query_vars) && empty($extra_query_vars) && empty($rewrite) && is_admin() && !isset($value)79–86apply_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–93apply_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–102apply_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–103apply_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–109apply_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–110apply_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–158apply_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–165apply_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–174apply_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–175apply_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–181apply_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–182apply_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

PHPCompiledExecutedBranchesNotes
8.6-dev48679–18268
8.548679–18268
8.448679–1826858 fewer instructions than PHP 8.3
8.354479–19968
8.254479–19968
8.154479–199684 fewer instructions than PHP 7.4
7.454879–19968

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:

  1. apply_filters( do_parse_request )filterline 148 (+12 into the body)

    Filters whether to parse the request.

  2. apply_filters( query_vars )filterline 311 (+175 into the body)

    Filters the query variables allowed before processing.

  3. apply_filters( request )filterline 409 (+273 into the body)

    Filters the array of parsed query variables.

  4. do_action( parse_request )actionline 418 (+282 into the body)

    Fires once all query variables for the current request have been parsed.

Uses · 15

Show all 15

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.

  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.

6.0.0
A return value was added.from the docblock
2.0.0
Introduced.from the docblock

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.