WordPress.org

Make WordPress Core

Opened 21 months ago

Last modified 9 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: Future Release Priority: normal
Severity: normal Version:
Component: Canonical Keywords: has-patch needs-refresh needs-unit-tests
Focuses: Cc:
PR Number:

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 21 months ago.

Download all attachments as: .zip

Change History (6)

@dimadin
21 months ago

#1 @dimadin
21 months ago

  • Keywords has-patch added

#2 @SergeyBiryukov
21 months ago

  • Milestone changed from Awaiting Review to 5.0

Related: #18084

#3 @peterwilsoncc
20 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.

#4 @johnbillion
12 months ago

  • Milestone changed from 5.0 to 5.1

#5 @pento
9 months ago

  • Milestone changed from 5.1 to Future Release

Patch needs refresh, and a whole lot of testing.

Note: See TracTickets for help on using tickets.