WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/29/2015 04:57:02 AM (5 years ago)
Author:
pento
Message:

Rewrite: Redirect attachment URLs when their slug changes.

Using the same logic that we use to redirect posts when their slug changes, we can provide the same functionality for attachments. Attachment pages are posts, too.

Props swissspdy.

Fixes #34043.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post-functions.php

    r34668 r34685  
    13311331 *              and `use_featured_image` labels.
    13321332 * @since 4.4.0 Added the `insert_into_item` and `uploaded_to_this_item` labels.
    1333  * 
     1333 *
    13341334 * @access private
    13351335 *
     
    32813281             */
    32823282            do_action( 'edit_attachment', $post_ID );
     3283            $post_after = get_post( $post_ID );
     3284
     3285            /**
     3286             * Fires once an existing attachment has been updated.
     3287             *
     3288             * @since 4.4.0
     3289             *
     3290             * @param int     $post_ID      Post ID.
     3291             * @param WP_Post $post_after   Post object following the update.
     3292             * @param WP_Post $post_before  Post object before the update.
     3293             */
     3294            do_action( 'attachment_updated', $post_ID, $post_after, $post_before );
    32833295        } else {
    32843296
     
    51345146 */
    51355147function wp_check_for_changed_slugs( $post_id, $post, $post_before ) {
    5136     // Don't bother if it hasnt changed.
    5137     if ( $post->post_name == $post_before->post_name )
     5148    // Don't bother if it hasn't changed.
     5149    if ( $post->post_name == $post_before->post_name ) {
    51385150        return;
     5151    }
    51395152
    51405153    // We're only concerned with published, non-hierarchical objects.
    5141     if ( $post->post_status != 'publish' || is_post_type_hierarchical( $post->post_type ) )
     5154    if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) {
    51425155        return;
    5143 
    5144     $old_slugs = (array) get_post_meta($post_id, '_wp_old_slug');
     5156    }
     5157
     5158    $old_slugs = (array) get_post_meta( $post_id, '_wp_old_slug' );
    51455159
    51465160    // If we haven't added this old slug before, add it now.
    5147     if ( !empty( $post_before->post_name ) && !in_array($post_before->post_name, $old_slugs) )
    5148         add_post_meta($post_id, '_wp_old_slug', $post_before->post_name);
     5161    if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs ) ) {
     5162        add_post_meta( $post_id, '_wp_old_slug', $post_before->post_name );
     5163    }
    51495164
    51505165    // If the new slug was used previously, delete it from the list.
    5151     if ( in_array($post->post_name, $old_slugs) )
    5152         delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
     5166    if ( in_array( $post->post_name, $old_slugs ) ) {
     5167        delete_post_meta( $post_id, '_wp_old_slug', $post->post_name );
     5168    }
    51535169}
    51545170
Note: See TracChangeset for help on using the changeset viewer.