[wp-trac] [WordPress Trac] #60675: Add hook for wp_check_for_changed_slugs()

WordPress Trac noreply at wordpress.org
Sat Mar 2 16:09:18 UTC 2024


#60675: Add hook for wp_check_for_changed_slugs()
-------------------------+-----------------------------
 Reporter:  Tkama        |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Permalinks   |    Version:  6.4.3
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 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
 <?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
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60675>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list