wp_check_for_changed_slugs

The timeline below displays how wordpress function wp_check_for_changed_slugs has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Checks 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 hasn't changed.
    if ($post->post_name == $post_before->post_name) {
        return;
    }
    // We're only concerned with published, non-hierarchical objects.
    if (!('publish' === $post->post_status || 'attachment' === get_post_type($post) && 'inherit' === $post->post_status) || 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, true)) {
        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, true)) {
        delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
    }
}

WordPress Version: 5.5

/**
 * 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 hasn't changed.
    if ($post->post_name == $post_before->post_name) {
        return;
    }
    // We're only concerned with published, non-hierarchical objects.
    if (!('publish' === $post->post_status || 'attachment' === get_post_type($post) && 'inherit' === $post->post_status) || 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, true)) {
        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, true)) {
        delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
    }
}

WordPress Version: 4.4

/**
 * 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 hasn't changed.
    if ($post->post_name == $post_before->post_name) {
        return;
    }
    // We're only concerned with published, non-hierarchical objects.
    if (!('publish' === $post->post_status || 'attachment' === get_post_type($post) && 'inherit' === $post->post_status) || 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);
    }
}

WordPress Version: 4.3

/**
 * 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);
    }
}

WordPress Version: 4.0

/**
 * 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
 * @return int Same as $post_id
 */
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);
    }
}

WordPress Version: 3.7

/**
 * Checked 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 object $post The Post Object
 * @param object $post_before The Previous Post Object
 * @return int Same as $post_id
 */
function wp_check_for_changed_slugs($post_id, $post, $post_before)
{
    // dont 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);
    }
}