WordPress.org

Make WordPress Core

Ticket #16557: 16557.3.diff

File 16557.3.diff, 2.0 KB (added by audrasjb, 13 months ago)

Canonical: Introduce hooks to filter redirect_guess_404_permalink() function’s behavior

  • src/wp-includes/canonical.php

    diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php
    index 4d373e13bd..a98ffc3b9b 100644
    a b function strip_fragment_from_url( $url ) { 
    692692function redirect_guess_404_permalink() {
    693693        global $wpdb;
    694694
     695        /**
     696         * Filters whether to do redirect guess of 404 requests.
     697         *
     698         * Returning a false value from the filter will disable redirect guess 404 permalink.
     699         * and return early.
     700         *
     701         * @since 5.5.0
     702         *
     703         * @param bool     $do_redirect_guess  Whether to do redirect guess 404 permalink. Default true.
     704         */
     705        if ( false === apply_filters( 'do_redirect_guess_404_permalink', true ) ) {
     706                return false;
     707        }
     708
     709        /**
     710         * Filters whether to short-circuit redirect guess of 404 requests.
     711         *
     712         * Return a non-false value from the filter will short-circuit the handling and return early.
     713         *
     714         * @since 5.5.0
     715         *
     716         * @param bool     $pre  Whether to short-circuit redirect guess 404 permalink. Default null.
     717         */
     718        $pre = apply_filters( 'pre_redirect_guess_404_permalink', null );
     719        if ( null !== $pre ) {
     720                return $pre;
     721        }
     722
    695723        if ( get_query_var( 'name' ) ) {
    696                 $where = $wpdb->prepare( 'post_name LIKE %s', $wpdb->esc_like( get_query_var( 'name' ) ) . '%' );
     724
     725                /**
     726                 * Filters whether to do a strict or loose guess.
     727                 *
     728                 * Returning true value from the filter will guess redirect only exact post_name matches.
     729                 *
     730                 * @since 5.5.0
     731                 *
     732                 * @param bool     $strict_guess  Whether to do a strict/exact guess. Default false.
     733                 */
     734                $strict_guess = apply_filters( 'strict_redirect_guess_404_permalink', false );
     735
     736                if ( $strict_guess ) {
     737                        $where = $wpdb->prepare( 'post_name = %s', get_query_var( 'name' ) );
     738                } else {
     739                        $where = $wpdb->prepare( 'post_name LIKE %s', $wpdb->esc_like( get_query_var( 'name' ) ) . '%' );
     740                }
    697741
    698742                // If any of post_type, year, monthnum, or day are set, use them to refine the query.
    699743                if ( get_query_var( 'post_type' ) ) {