wp_check_for_changed_slugs()判断文章/页面的别名是否已经修改

目录

描述

译文

检查已发表文章中经改动的缩略名并保存原有缩略名。

该函数与文章数据同时使用。函数检查wp-old-slug文章字段,并且只集中于已发表文章和已更改的缩略名。

若缩略名已被更改且不再存在于原有缩略名中时,将该缩略名添加到文章meta字段中('_wp_old_slug')以保存文章的曾用缩略名。

该函数的最常见用法是重定向经过修改的文章,因此连接到修改前文章的链接都将被重定向到修改后的文章。

原文

检查已发表文章中经改动的缩略名并保存原有缩略名。

该函数与文章数据同时使用。函数检查wp-old-slug文章字段,并且只集中于已发表文章和已更改的缩略名。

若缩略名已被更改且不再存在于原有缩略名中时,将该缩略名添加到文章meta字段中('_wp_old_slug')以保存文章的曾用缩略名。

该函数的最常见用法是重定向经过修改的文章,因此连接到修改前文章的链接都将被重定向到修改后的文章。

wp_check_for_changed_slugs() 描述

用法

<?php wp_check_for_changed_slugs$post_id ); ?>

wp_check_for_changed_slugs() 用法

参数

$post_id

(integer) (必填) Post ID.

默认值: None

wp_check_for_changed_slugs() 参数

返回值

(integer) 

Same as $post_id

wp_check_for_changed_slugs() 返回值

注意

wp_check_for_changed_slugs() 注意

历史

添加于 版本: 2.1.0

wp_check_for_changed_slugs() 历史

源文件

wp_check_for_changed_slugs() 函数的代码位于 wp-includes/post.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Check for changed slugs for published post objects and save the old slug.
 *
 * The function is used when a post object of any type is updated,
 * by comparing the current and previous post objects.
 *
 * If the slug was changed and not already part of the old slugs then it will be
 * added to the post meta field ('_wp_old_slug') for storing old slugs for that
 * post.
 *
 * The most logically usage of this function is redirecting changed post objects, so
 * that those that linked to an changed post will be redirected to the new post.
 *
 * @since 2.1.0
 *
 * @param int     $post_id     Post ID.
 * @param WP_Post $post        The Post Object
 * @param WP_Post $post_before The Previous Post Object
 */
function wp_check_for_changed_slugs( $post_id, $post, $post_before ) {
	// Don't bother if it hasnt changed.
	if ( $post->post_name == $post_before->post_name )
		return;

	// We're only concerned with published, non-hierarchical objects.
	if ( $post->post_status != 'publish' || is_post_type_hierarchical( $post->post_type ) )
		return;

	$old_slugs = (array) get_post_meta($post_id, '_wp_old_slug');

	// If we haven't added this old slug before, add it now.
	if ( !empty( $post_before->post_name ) && !in_array($post_before->post_name, $old_slugs) )
		add_post_meta($post_id, '_wp_old_slug', $post_before->post_name);

	// If the new slug was used previously, delete it from the list.
	if ( in_array($post->post_name, $old_slugs) )
		delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
}

wp_check_for_changed_slugs() 源文件