[wp-trac] [WordPress Trac] #52373: URL returned by get_attachment_link() can 404.
WordPress Trac
noreply at wordpress.org
Mon Jan 25 23:51:23 UTC 2021
#52373: URL returned by get_attachment_link() can 404.
---------------------------+-----------------------------
Reporter: peterwilsoncc | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Media | Version: 4.4
Severity: normal | Keywords:
Focuses: |
---------------------------+-----------------------------
The permalink returned by `get_attachment_link()` and `get_permalink( /*
attachment */ )` can result in a file not found page in the following
circumstances:
* The attachment's post parent has been deleted but `post_parent` is set
(ie, post parent is pointing to an invalid ID).
* The attachment's post parent is an unregistered post type.
In both cases `get_permalink()` will return a URL in the format `w.org
/attachment-name` that will 404 when visited on the front end of the
website.
If pretty URLs are enabled, the URL should return `w.org/attachment
/attachment-name` as the permalink.
Adding the following unit test will demonstrate the bug:
{{{#!php
<?php
public function
test_attachment_attached_to_non_existent_post_type_does_not_404() {
global $wp_post_types;
$this->set_permalink_structure(
'/%year%/%monthnum%/%day%/%postname%/' );
flush_rewrite_rules();
// Create temporay post type.
register_post_type( 'not_a_post_type', array( 'public' => true )
);
$post_id = self::factory()->post->create( array( 'post_type' =>
'not_a_post_type' ) );
// Attach media to post of post type.
$attachment_id = self::factory()->attachment->create_object(
'image.jpg',
$post_id,
array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
'post_title' => 'An Attachment!',
'post_status' => 'inherit',
)
);
// Unregister post type.
foreach ( $wp_post_types as $id => $pt ) {
if ( 'not_a_post_type' === $pt->name ) {
unset( $wp_post_types[ $id ] );
break;
}
}
// Visit permalink.
$this->go_to( get_permalink( $attachment_id ) );
$this->assertQueryTrue( 'is_attachment' );
}
}}}
This looks to have been introduced in #1914
--
Ticket URL: <https://core.trac.wordpress.org/ticket/52373>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list