WordPress.org

Make WordPress Core

Opened 3 months ago

Last modified 3 months ago

#43274 new defect (bug)

Changing $(comments|feed)_base of WP_Rewrite causes errors because of hardcoded strings in redirect_canonical()

Reported by: dimadin Owned by:
Milestone: 5.0 Priority: normal
Severity: normal Version:
Component: Canonical Keywords: has-patch needs-refresh needs-unit-tests
Focuses: Cc:

Description

When you change $comments_base/$feed_base for WP_Rewrite instance, URLs with that base will not work because comments and feed strings are hardcoded in a few places in redirect_canonical(). Changing these to use $wp_rewrite->comments_base and $wp_rewrite->feed_base respectively will fix this issue.

NOTE: When testing feed URLs, changing feed with feed_base in example.com/feed/ will not work. Feed URLs work in two places: example.com/(feed|rss2?|rdf|atom) and example.com/feed_base/(feed|rss2?|rdf|atom) (see rules). When you open example.com/feed/ you match first rule where feed is type of feed. You need second rule to test it.

There is quick workaround though:

add_action( 'after_setup_theme', function() {
        $GLOBALS['wp_rewrite']->feed_base = 'mycustomfeedbase';
        $GLOBALS['wp_rewrite']->feeds[] = 'mycustomfeedbase';
} );

add_action( 'parse_query', function( $q ) {
        if ( $GLOBALS['wp_rewrite']->feed_base == $q->get( 'feed' ) ) {
                $q->set( 'feed', 'feed' );
        }
} );

Don't forget to flush rewrite rules.

Attachments (1)

43274.diff (2.9 KB) - added by dimadin 3 months ago.

Download all attachments as: .zip

Change History (4)

@dimadin
3 months ago

#1 @dimadin
3 months ago

  • Keywords has-patch added

#2 @SergeyBiryukov
3 months ago

  • Milestone changed from Awaiting Review to 5.0

Related: #18084

#3 @peterwilsoncc
3 months ago

  • Keywords needs-refresh needs-unit-tests added

@dimadin

Are you able to wrap the variables in regular expressions with preg_quote() to ensure they're escaped correctly?

Some tests to ensure the function behaves as expected with custom bases would be great to see too.

Note: See TracTickets for help on using tickets.