Make WordPress Core


Ignore:
Timestamp:
09/20/2022 01:10:24 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Bootstrap/Load: Send HTTP headers after querying posts in WP::main().

By running WP::send_headers() after posts have been queried, we can ensure that conditional tags like is_front_page(), is_home(), etc. work as expected.

This provides better context and more flexibility when adjusting HTTP headers via the wp_headers filter or send_headers action.

Previously, the earliest action where conditional tags worked correctly was wp.

Includes moving the X-Pingback header, previously sent in WP::handle_404()​ after posts have been queried, to a more appropriate place in WP::send_headers().

Follow-up to [2627], [34442].

Props jonoaldersonwp, joostdevalk, peterwilsoncc, adamsilverstein, SergeyBiryukov.
Fixes #56068.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp.php

    r54133 r54250  
    409409     *
    410410     * @since 2.0.0
    411      * @since 4.4.0 `X-Pingback` header is added conditionally after posts have been queried in handle_404().
     411     * @since 4.4.0 `X-Pingback` header is added conditionally for single posts that allow pings.
     412     * @since 6.1.0 Runs after posts have been queried.
    412413     */
    413414    public function send_headers() {
     
    505506        }
    506507
     508        if ( is_singular() ) {
     509            $post = isset( $wp_query->post ) ? $wp_query->post : null;
     510
     511            // Only set X-Pingback for single posts that allow pings.
     512            if ( $post && pings_open( $post ) ) {
     513                $headers['X-Pingback'] = get_bloginfo( 'pingback_url', 'display' );
     514            }
     515        }
     516
    507517        /**
    508518         * Filters the HTTP headers before they're sent to the browser.
     
    702712            if ( is_singular() ) {
    703713                $post = isset( $wp_query->post ) ? $wp_query->post : null;
    704 
    705                 // Only set X-Pingback for single posts that allow pings.
    706                 if ( $post && pings_open( $post ) && ! headers_sent() ) {
    707                     header( 'X-Pingback: ' . get_bloginfo( 'pingback_url', 'display' ) );
    708                 }
     714                $next = '<!--nextpage-->';
    709715
    710716                // Check for paged content that exceeds the max number of pages.
    711                 $next = '<!--nextpage-->';
    712717                if ( $post && ! empty( $this->query_vars['page'] ) ) {
    713718                    // Check if content is actually intended to be paged.
     
    771776        $parsed = $this->parse_request( $query_args );
    772777
    773         $this->send_headers();
    774 
    775778        if ( $parsed ) {
    776779            $this->query_posts();
     
    779782        }
    780783
     784        $this->send_headers();
     785
    781786        /**
    782787         * Fires once the WordPress environment has been set up.
Note: See TracChangeset for help on using the changeset viewer.