将旧slu red重定向到正确的永久链接。

原型

wp_old_slug_redirect()

描述

尝试从过去的slu slu中找到当前的slu ..

源文件

路径:wp-includes/query.php

<?php
...
function wp_old_slug_redirect() {
	if ( is_404() && '' !== get_query_var( 'name' ) ) {
		// Guess the current post_type based on the query vars.
		if ( get_query_var( 'post_type' ) ) {
			$post_type = get_query_var( 'post_type' );
		} elseif ( get_query_var( 'attachment' ) ) {
			$post_type = 'attachment';
		} elseif ( get_query_var( 'pagename' ) ) {
			$post_type = 'page';
		} else {
			$post_type = 'post';
		}
		if ( is_array( $post_type ) ) {
			if ( count( $post_type ) > 1 ) {
				return;
			}
			$post_type = reset( $post_type );
		}
		// Do not attempt redirect for hierarchical post types
		if ( is_post_type_hierarchical( $post_type ) ) {
			return;
		}
		$id = _find_post_by_old_slug( $post_type );
		if ( ! $id ) {
			$id = _find_post_by_old_date( $post_type );
		}
		/**
		 * Filters the old slug redirect post ID.
		 *
		 * @since 4.9.3
		 *
		 * @param int $id The redirect post ID.
		 */
		$id = apply_filters( 'old_slug_redirect_post_id', $id );
		if ( ! $id ) {
			return;
		}
		$link = get_permalink( $id );
		if ( get_query_var( 'paged' ) > 1 ) {
			$link = user_trailingslashit( trailingslashit( $link ) . 'page/' . get_query_var( 'paged' ) );
		} elseif( is_embed() ) {
			$link = user_trailingslashit( trailingslashit( $link ) . 'embed' );
		}
		/**
		 * Filters the old slug redirect URL.
		 *
		 * @since 4.4.0
		 *
		 * @param string $link The redirect URL.
		 */
		$link = apply_filters( 'old_slug_redirect_url', $link );
		if ( ! $link ) {
			return;
		}
		wp_redirect( $link, 301 ); // Permanent redirect
		exit;
	}
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/wp_old_slug_redirect/