WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/29/2015 09:41:14 AM (6 years ago)
Author:
pento
Message:

Permalinks: Add pretty permalinks for unattached attachments.

Previously, unattached attachments would have unsightly /?attachment_id=1 URLs. As we've moved away from attachments being specifically attached to posts, instead being Media items, this has made the unattached URLs a more common occurrence.

We can breath easy once more, knowing that the world is a little bit safer from the horror of unnecessarily ugly URLs.

Props SergeyBiryukov, wonderboymusic, pento.

Fixes #1914.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/link.php

    r34088 r34690  
    396396        $this->assertEquals( $non_pretty_permalink, get_permalink( $p ) );
    397397    }
     398
     399    /**
     400     * @ticket 1914
     401     */
     402    public function test_unattached_attachment_has_a_pretty_permalink() {
     403        global $wp_rewrite;
     404        $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
     405        $wp_rewrite->flush_rules();
     406
     407        $attachment_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(
     408            'post_mime_type' => 'image/jpeg',
     409            'post_type' => 'attachment',
     410            'post_title' => 'An Attachment!',
     411            'post_status' => 'inherit',
     412        ) );
     413
     414        $attachment = get_post( $attachment_id );
     415
     416        $this->assertSame( home_url( user_trailingslashit( $attachment->post_name ) ), get_permalink( $attachment_id ) );
     417    }
     418
     419    /**
     420     * @ticket 1914
     421     */
     422    public function test_attachment_attached_to_non_existent_post_type_has_a_pretty_permalink() {
     423        global $wp_rewrite, $wp_post_types;
     424        $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
     425
     426        register_post_type( 'not_a_post_type', array( 'public' => true ) );
     427
     428        $wp_rewrite->flush_rules();
     429
     430        $post_id = $this->factory->post->create( array( 'post_type' => 'not_a_post_type' ) );
     431
     432        $attachment_id = $this->factory->attachment->create_object( 'image.jpg', $post_id, array(
     433            'post_mime_type' => 'image/jpeg',
     434            'post_type' => 'attachment',
     435            'post_title' => 'An Attachment!',
     436            'post_status' => 'inherit',
     437        ) );
     438
     439        $attachment = get_post( $attachment_id );
     440
     441        $this->assertSame( get_permalink( $post_id ) . user_trailingslashit( $attachment->post_name ), get_permalink( $attachment_id ) );
     442
     443        foreach( $wp_post_types as $id => $pt ) {
     444            if ( 'not_a_post_type' === $pt->name ) {
     445                unset( $wp_post_types[ $id ] );
     446                break;
     447            }
     448        }
     449
     450        $this->assertSame( home_url( user_trailingslashit( $attachment->post_name ) ), get_permalink( $attachment_id ) );
     451    }
    398452}
Note: See TracChangeset for help on using the changeset viewer.