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

WordPress Trac noreply at wordpress.org
Thu Feb 25 15:40:11 UTC 2021


#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        |  Resolution:
 Keywords:  has-patch     |     Focuses:
--------------------------+------------------------------

Comment (by vaurdan):

 @swissspidy I think I understand the issue. However we still have a few
 sites with this behavior, and I believe this issue should still be
 addressed, as it is causing inconsistencies as well.

 If I understand correctly, the issue is after the native sitemaps are
 disabled, `wp-sitemap.xml` will return a 200 with the homepage, instead of
 the expected 404. If we flush the rewrite rules when sitemaps are disabled
 and the rules are still there, would it work?

 Something like:

 {{{#!php
 <?
 public function init() {
         if ( ! $this->sitemaps_enabled() ) {
                 $this->maybe_flush_rewrites();
                 return;
         }

         // These will all fire on the init hook.
         $this->register_rewrites();

         add_action( 'template_redirect', array( $this, 'render_sitemaps' )
 );

         $this->register_sitemaps();

         // Add additional action callbacks.
         add_filter( 'pre_handle_404', array( $this, 'redirect_sitemapxml'
 ), 10, 2 );
         add_filter( 'robots_txt', array( $this, 'add_robots' ), 0, 2 );
 }

 private function maybe_flush_rewrites() {
         global $wp_rewrite;

         if ( ! $this->sitemaps_enabled() && array_key_exists( '^wp-
 sitemap\.xml$', $wp_rewrite->extra_rules_top ) ) {
                 flush_rewrite_rules();
         }
 }
 }}}

 This is a very crude solution, but it might work. Flushing will only
 happen once, after the native sitemaps are disabled, and should allow us
 to move the `$this->sitemaps_enabled()` to the top of the `init()`
 function, as suggested on the patch, which will fix this bug.

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


More information about the wp-trac mailing list