Skip to content

Commit

Permalink
Bootstrap/Load: Send HTTP headers after querying posts in WP::main().
Browse files Browse the repository at this point in the history
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.
Built from https://develop.svn.wordpress.org/trunk@54250


git-svn-id: https://core.svn.wordpress.org/trunk@53809 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
SergeyBiryukov committed Sep 20, 2022
1 parent 4f85188 commit 2886625
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions wp-includes/class-wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ public function parse_request( $extra_query_vars = '' ) {
* If showing a feed, it will also send Last-Modified, ETag, and 304 status if needed.
*
* @since 2.0.0
* @since 4.4.0 `X-Pingback` header is added conditionally after posts have been queried in handle_404().
* @since 4.4.0 `X-Pingback` header is added conditionally for single posts that allow pings.
* @since 6.1.0 Runs after posts have been queried.
*/
public function send_headers() {
$headers = array();
Expand Down Expand Up @@ -504,6 +505,15 @@ public function send_headers() {
}
}

if ( is_singular() ) {
$post = isset( $wp_query->post ) ? $wp_query->post : null;

// Only set X-Pingback for single posts that allow pings.
if ( $post && pings_open( $post ) ) {
$headers['X-Pingback'] = get_bloginfo( 'pingback_url', 'display' );
}
}

/**
* Filters the HTTP headers before they're sent to the browser.
*
Expand Down Expand Up @@ -701,14 +711,9 @@ public function handle_404() {

if ( is_singular() ) {
$post = isset( $wp_query->post ) ? $wp_query->post : null;

// Only set X-Pingback for single posts that allow pings.
if ( $post && pings_open( $post ) && ! headers_sent() ) {
header( 'X-Pingback: ' . get_bloginfo( 'pingback_url', 'display' ) );
}
$next = '<!--nextpage-->';

// Check for paged content that exceeds the max number of pages.
$next = '<!--nextpage-->';
if ( $post && ! empty( $this->query_vars['page'] ) ) {
// Check if content is actually intended to be paged.
if ( false !== strpos( $post->post_content, $next ) ) {
Expand Down Expand Up @@ -770,14 +775,14 @@ public function main( $query_args = '' ) {

$parsed = $this->parse_request( $query_args );

$this->send_headers();

if ( $parsed ) {
$this->query_posts();
$this->handle_404();
$this->register_globals();
}

$this->send_headers();

/**
* Fires once the WordPress environment has been set up.
*
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.1-alpha-54249';
$wp_version = '6.1-alpha-54250';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit 2886625

Please sign in to comment.