[wp-trac] [WordPress Trac] #51136: Third-party sitemap plugin returning 404 after disabling core's sitemap

WordPress Trac noreply at wordpress.org
Tue Aug 25 11:38:17 UTC 2020


#51136: Third-party sitemap plugin returning 404 after disabling core's sitemap
--------------------------+-----------------------------
 Reporter:  vaurdan       |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Sitemaps      |    Version:  5.5
 Severity:  normal        |   Keywords:  has-patch
  Focuses:                |
--------------------------+-----------------------------
 When using a third-party sitemap plugin (such as
 [https://github.com/Automattic/msm-sitemap MSM Sitemap]), sitemaps are
 returning HTTP status code 404, despite outputting the actual sitemap XML.

 Core's sitemap is being disabled using the `wp_sitemaps_enabled` filter,
 however the 404 status code is still being sent in the response.

 I believe the issue resides in `WP_Sitemaps::init()`, where both the
 rewrite rules and the template redirect are being set before the actual
 `$this->sitemaps_enabled()` check
 ([https://github.com/WordPress/WordPress/blob/5.5/wp-includes/sitemaps
 /class-wp-sitemaps.php#L65-L72 see here]).

 Since both Core and `msm-sitemap` use `sitemap` parameter for the rewrite,
 `WP_Sitemaps::render_sitemaps` will still be called on a `sitemap.xml`
 request, and since Core sitemap is disabled (`$this->sitemaps_enabled()`
 is false), it will return a 404 error, despite `msm-sitemap` generating
 and outputting the correct XML:

 {{{#!php
 if ( ! $this->sitemaps_enabled() ) {
         $wp_query->set_404();
         status_header( 404 );
         return;
 }
 }}}

 I have came with the following workaround to prevent the 404 status code
 on sitemap requests:

 {{{#!php
 <?php
 // Assuming that `wp_sitemaps_enabled` was already filtered to `false`
 add_action('init', function() {
         global $wp_sitemaps;
         remove_action( 'template_redirect', array( $wp_sitemaps,
 'render_sitemaps' ) );
 }, 100 );

 }}}

 I'm including a patch with a fix suggestion, that is moving the `if ( !
 $this->sitemaps_enabled() )` validation to the beginning of the
 `WP_Sitemaps::init` method.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51136>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list