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.

Parameters

$extra_query_varsarray|stringoptional
Set the extra query variables.Default: ''

Return

bool
Whether the request was parsed.

Hooks 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

	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;

History

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.