WordPress.org

Make WordPress Core

Changeset 47727


Ignore:
Timestamp:
04/30/2020 12:03:11 PM (7 months ago)
Author:
SergeyBiryukov
Message:

Canonical: Redirect paged requests for non-paginated posts to the post permalink.

This avoids displaying duplicate content of the same post under different URLs and ensures the canonical URL is correct.

Previously, requests for invalid page numbers were only redirected to the post permalink if the post was actually paginated using the <!--nextpage--> marker.

Follow-up to [34492].

Props jeremyfelt, prografika, sachit.tandukar, subrataemfluence, hronak, ekatherine, henry.wright, chesio, dd32, SergeyBiryukov.
Fixes #40773. See #45337, #28081, #11694.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/canonical.php

    r47617 r47727  
    158158        }
    159159
    160         if ( get_query_var( 'page' ) && $wp_query->post &&
    161             false !== strpos( $wp_query->post->post_content, '<!--nextpage-->' ) ) {
     160        if ( get_query_var( 'page' ) && $wp_query->post ) {
    162161            $redirect['path']  = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
    163162            $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
  • trunk/src/wp-includes/class-wp.php

    r47550 r47727  
    679679                // Check for paged content that exceeds the max number of pages.
    680680                $next = '<!--nextpage-->';
    681                 if ( $p && false !== strpos( $p->post_content, $next ) && ! empty( $this->query_vars['page'] ) ) {
    682                     $page    = trim( $this->query_vars['page'], '/' );
    683                     $success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 );
     681                if ( $p && ! empty( $this->query_vars['page'] ) ) {
     682                    // Check if content is actually intended to be paged.
     683                    if ( false !== strpos( $p->post_content, $next ) ) {
     684                        $page    = trim( $this->query_vars['page'], '/' );
     685                        $success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 );
     686                    } else {
     687                        $success = false;
     688                    }
    684689                }
    685690            }
  • trunk/tests/phpunit/tests/canonical.php

    r47122 r47727  
    176176            array( '/2008/09/03/images-te?page=3', '/2008/09/03/images-test/3/' ),
    177177
     178            array( '/2008/03/03/comment-test/3/', '/2008/03/03/comment-test/' ),
     179            array( '/2008/03/03/comment-test/?page=3', '/2008/03/03/comment-test/' ),
     180
    178181            // Comments.
    179182            array( '/2008/03/03/comment-test/?cpage=2', '/2008/03/03/comment-test/comment-page-2/' ),
  • trunk/tests/phpunit/tests/link/wpGetCanonicalURL.php

    r46586 r47727  
    1212        self::$post_id = $factory->post->create(
    1313            array(
    14                 'post_status' => 'publish',
     14                'post_content' => 'Page 1 <!--nextpage--> Page 2 <!--nextpage--> Page 3',
     15                'post_status'  => 'publish',
    1516            )
    1617        );
Note: See TracChangeset for help on using the changeset viewer.