Make WordPress Core

Ticket #19876: bare-category-redirect.2.diff

File bare-category-redirect.2.diff, 3.3 KB (added by duck_, 13 years ago)
  • wp-includes/canonical.php

     
    9393                }
    9494
    9595                if ( ! $redirect_url )
    96                         $redirect_url = redirect_guess_404_permalink();
     96                        $redirect_url = redirect_guess_404_permalink( $requested_url );
    9797
    9898        } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
    9999                // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
     
    417417}
    418418
    419419/**
    420  * Attempts to guess correct post based on query vars.
     420 * Attempts to guess the correct URL from the current URL (that produced a 404) or
     421 * the current query variables.
    421422 *
    422423 * @since 2.3.0
    423424 * @uses $wpdb
    424425 *
    425  * @return bool|string Returns False, if it can't find post, returns correct
    426  *              location on success.
     426 * @param string $current_url Optional. The URL that has 404'd.
     427 * @return bool|string The correct URL if one is found. False on failure.
    427428 */
    428 function redirect_guess_404_permalink() {
    429         global $wpdb;
     429function redirect_guess_404_permalink( $current_url = '' ) {
     430        global $wpdb, $wp_rewrite;
    430431
    431         if ( !get_query_var('name') )
    432                 return false;
     432        $parsed_url = @parse_url( $current_url );
    433433
    434         $where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%');
     434        // Attempt to redirect bare category slugs if the permalink structure starts
     435        // with the %category% tag.
     436        if ( isset( $parsed_url['path'] )
     437                && preg_match( '#^[^%]+%category%#', $wp_rewrite->permalink_structure )
     438                && $cat = get_category_by_path( $parsed_url['path'] )
     439        ) {
     440                if ( ! is_wp_error( $cat ) )
     441                        return get_term_link( $cat );
     442        }
    435443
    436         // if any of post_type, year, monthnum, or day are set, use them to refine the query
    437         if ( get_query_var('post_type') )
    438                 $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type'));
    439         if ( get_query_var('year') )
    440                 $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
    441         if ( get_query_var('monthnum') )
    442                 $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
    443         if ( get_query_var('day') )
    444                 $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
     444        if ( get_query_var('name') ) {
     445                $where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%');
    445446
    446         $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
    447         if ( !$post_id )
    448                 return false;
    449         return get_permalink($post_id);
     447                // if any of post_type, year, monthnum, or day are set, use them to refine the query
     448                if ( get_query_var('post_type') )
     449                        $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type'));
     450                if ( get_query_var('year') )
     451                        $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
     452                if ( get_query_var('monthnum') )
     453                        $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
     454                if ( get_query_var('day') )
     455                        $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
     456
     457                $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
     458                if ( ! $post_id )
     459                        return false;
     460                return get_permalink( $post_id );
     461        }
     462
     463        return false;
    450464}
    451465
    452466add_action('template_redirect', 'redirect_canonical');