• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / WP::parse_request() – Parse request to find correct WordPress query.

You appear to be a bot. Output may be restricted

Description

Parses the request to find the correct WordPress query.

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.

Usage

WP::parse_request( $extra_query_vars );

Parameters

$extra_query_vars
( array|string ) optional – Set the extra query variables.

Returns

void

Source

File name: wordpress/wp-includes/class-wp.php
Lines:

1 to 100 of 257
  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           $this             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;
    }

    $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         = isset( $_SERVER['PATH_INFO'] ) ? $_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       = trim( parse_url( home_url(), PHP_URL_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, '/' );
      $req_uri  = preg_replace( $home_path_regex, '', $req_uri );
      $req_uri  = trim( $req_uri, '/' );
      $pathinfo = trim( $pathinfo, '/' );
      $pathinfo = preg_replace( $home_path_regex, '', $pathinfo );
      $pathinfo = trim( $pathinfo, '/' );
      $self     = trim( $self, '/' );
      $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;

      $this->request = $requested_path;

      // Look for matches.
      $request_match = $requested_path;
      if ( empty( $request_match ) ) {
        // An empty request could only match against ^$ regex.
        if ( isset( $rewrite['$'] ) ) {
          $this->matched_rule = '$';
          $query              = $rewrite['$'];
          $matches            = array( '' );
        }
      } else {
        foreach ( (array) $rewrite as $match => $query ) {
          // If the requested file is the anchor of the match, prepend it to the path info.
          if ( ! empty( $requested_file ) && strpos( $match, $requested_file ) === 0 && $requested_file != $requested_path ) {
            $request_match = $requested_file . '/' . $requested_path;
          }

          if ( preg_match( "#^$match#", $request_match, $matches ) ||
            preg_match( "#^$match#", urldecode( $request_match ), $matches ) ) {

            if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
              // This is a verbose page match, let's check to be sure about it.
              $page = get_page_by_path( $matches[ $varmatch[1] ] );
              if ( ! $page ) {
                continue;
              }

 
[1] [2] [3] Next »

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 21st August 2020

Primary Sidebar

Information

Function name: WP::parse_request
Class ref: WP
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-includes/class-wp.php
File ref: wp-includes/class-wp.php
Deprecated?: No
API Letters: P,R,W

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites