WordPress.org

Make WordPress Core

Changeset 34659


Ignore:
Timestamp:
09/28/2015 06:56:54 AM (4 years ago)
Author:
pento
Message:

Rewrite: When redirecting old slugs, include URL endpoints.

Historically, wp_old_slug_redirect() has only ever redirected the old slug of posts, it hasn't included URL endpoints, or worked with comment feed URLs. By adding support for these, we ensure a greater range of URLs aren't killed when the slug changes.

Props swissspdy.

Fixes #33920.

Location:
trunk
Files:
1 added
1 edited

Legend:

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

    r34502 r34659  
    47264726 * @since 2.1.0
    47274727 *
    4728  * @global WP_Query $wp_query Global WP_Query instance.
    4729  * @global wpdb     $wpdb     WordPress database abstraction object.
     4728 * @global WP_Query   $wp_query   Global WP_Query instance.
     4729 * @global wpdb       $wpdb       WordPress database abstraction object.
     4730 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
    47304731 */
    47314732function wp_old_slug_redirect() {
    4732     global $wp_query;
    4733     if ( is_404() && '' != $wp_query->query_vars['name'] ) :
     4733    global $wp_query, $wp_rewrite;
     4734
     4735    if ( '' !== $wp_query->query_vars['name'] ) :
    47344736        global $wpdb;
    47354737
     
    47684770            return;
    47694771
    4770         $link = get_permalink($id);
    4771 
    4772         if ( !$link )
     4772        $link = get_permalink( $id );
     4773
     4774        if ( is_feed() ) {
     4775            $link = user_trailingslashit( trailingslashit( $link ) . 'feed' );
     4776        } elseif ( isset( $GLOBALS['wp_query']->query_vars['paged'] ) && $GLOBALS['wp_query']->query_vars['paged'] > 1 ) {
     4777            $link = user_trailingslashit( trailingslashit( $link ) . 'page/' . $GLOBALS['wp_query']->query_vars['paged'] );
     4778        } elseif ( is_404() ) {
     4779            // Add rewrite endpoints if necessary.
     4780            foreach ( $wp_rewrite->endpoints as $endpoint ) {
     4781                if ( $endpoint[2] && false !== get_query_var( $endpoint[2], false ) ) {
     4782                    $link = user_trailingslashit( trailingslashit( $link ) . $endpoint[1] );
     4783                }
     4784            }
     4785        }
     4786
     4787        /**
     4788         * Filter the old slug redirect URL.
     4789         *
     4790         * @since 4.4.0
     4791         *
     4792         * @param string $link The redirect URL.
     4793         */
     4794        $link = apply_filters( 'old_slug_redirect_url', $link );
     4795
     4796        if ( ! $link ) {
    47734797            return;
     4798        }
    47744799
    47754800        wp_redirect( $link, 301 ); // Permanent redirect
Note: See TracChangeset for help on using the changeset viewer.