Make WordPress Core

Ticket #20902: 20902-fix-redirect-canonical-query-vars.patch

File 20902-fix-redirect-canonical-query-vars.patch, 1.6 KB (added by sachinrajcp123, 7 months ago)
  • src/wp-includes/canonical.php

    diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php
    index 12fdd1a..98a4e3c 100644
    a b function redirect_canonical( $requested_url = null, $do_redirect = true ) { 
    374374                }
    375375        }
    376376
     377        /*
     378         * Enhancement: Support redirecting query vars like ?pagename=blog,
     379         * ?author_name=admin, and ?post_format=image to their canonical permalink forms.
     380         */
     381
     382        if ( isset( $_GET['pagename'] ) && ! empty( $_GET['pagename'] ) ) {
     383                $page = get_page_by_path( sanitize_title( $_GET['pagename'] ) );
     384                if ( $page && ! is_wp_error( $page ) ) {
     385                        $redirect_url = get_permalink( $page->ID );
     386                        if ( $redirect_url && $redirect_url !== $requested_url ) {
     387                                wp_redirect( $redirect_url, 301 );
     388                                exit;
     389                        }
     390                }
     391        }
     392
     393        if ( isset( $_GET['author_name'] ) && ! empty( $_GET['author_name'] ) ) {
     394                $user = get_user_by( 'slug', sanitize_title( $_GET['author_name'] ) );
     395                if ( $user && ! is_wp_error( $user ) ) {
     396                        $redirect_url = get_author_posts_url( $user->ID );
     397                        if ( $redirect_url && $redirect_url !== $requested_url ) {
     398                                wp_redirect( $redirect_url, 301 );
     399                                exit;
     400                        }
     401                }
     402        }
     403
     404        if ( isset( $_GET['post_format'] ) && ! empty( $_GET['post_format'] ) ) {
     405                $term = get_term_by( 'slug', sanitize_title( $_GET['post_format'] ), 'post_format' );
     406                if ( $term && ! is_wp_error( $term ) ) {
     407                        $redirect_url = get_term_link( $term );
     408                        if ( $redirect_url && $redirect_url !== $requested_url ) {
     409                                wp_redirect( $redirect_url, 301 );
     410                                exit;
     411                        }
     412                }
     413        }
     414
    377415        // Canonical redirect was not applicable
    378416        return false;
    379417}