Make WordPress Core

Opened 9 months ago

Last modified 8 months ago

#60675 new enhancement

Add hook for wp_check_for_changed_slugs()

Reported by: tkama's profile Tkama Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.4.3
Component: Permalinks Keywords:
Focuses: Cc:

Description

The wp_check_for_changed_slugs() function adds old post slug (post_name) to the list is post_name was changed. And it do it always without "make sense" check.

For example I just publish a new post and after a couple of minutes change the post_name (slug), because I forgot to change it before publishing. And the old slug is added to the list, but this make no sense, because this is new post - it newer indexed yet and newer pushed somewhere (if I don't have auto publish to some social network functionality), so the saved post slug meke no sense here.

I think there should be possibility to hook the function and disable it after custom checks.

I suggest to add the following hook:

<?php
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;
        }

        /**
         * Allowes to stop {@see wp_check_for_changed_slugs()} function functionality.
         *
         * @param bool    $is_skip      If true the {@see wp_check_for_changed_slugs()} will be stopped.
         * @param WP_Post $post         The post object.
         * @param WP_Post $post_before  The previous post object.
         */
        if ( apply_filters( 'skip_wp_check_for_changed_slugs', false, $post, $post_before ) ) {
                return;
        }

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

        //  The rest of the function code
}

Attachments (1)

patch.patch (974 bytes) - added by Tkama 9 months ago.

Download all attachments as: .zip

Change History (1)

@Tkama
9 months ago

Note: See TracTickets for help on using tickets.