Make WordPress Core

Opened 9 months ago

Last modified 9 months ago

#52324 new defect (bug)

Sample permalinks unavailable for attachment pages.

Reported by: peterwilsoncc Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Media Keywords: close
Focuses: Cc:


Both get_page_link() and get_post_permalink() accept a third parameter $sample to generate permalinks for unpublished posts (draft, future, etc).

get_attachment_link() does not include this third parameter and will always generate an ugly formatted link for attachment pages.

For example instead of w.org/future-post/future-attachment the attachment page is always shown as w.org/?attachment_id=4.

To reproduce:

  1. Create a post leave it in draft
  2. Upload some new media
  3. Link it to the attachment page
  4. Observe that it uses the ugly URL format.

Change History (2)

#1 @Mista-Flo
9 months ago


I can reproduce the issue.

I tried some stuff, and the issue does not seem to come from get_attachment_link function. After some debugging, I realised it's because the parent link (post link) is not a pretty permalink yet, so it will be http://localhost:8889/?post_type=post&p=393 for example.

Then in get_attachment_link, it does not accept a parent link like this, so it will fallback and return something like: w.org/?attachment_id=4.

So the issue is rather on:

        if ( $wp_rewrite->using_permalinks() && $parent ) {
                if ( 'page' === $parent->post_type ) {
                        $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front.
                } else {
                        $parentlink = get_permalink( $post->post_parent );

get_permalink call does not return a pretty URL for a draft post parent.

#2 @peterwilsoncc
9 months ago

  • Keywords close added

Thanks for looking in to this.

For posts published immediately it looks like _fix_attachment_links() resolves this. As I was testing scheduled posts at the time it looks like I hit #36976 so it's possible this ticket can be closed as a near enough duplicate of that.

While investigating #52373, I was considering changin the default permalink to /attachment/{$attachment->post_name} but that would require rewriting _fix_attachment_links() so it might be a terrible idea.

Adding the close keyword for the media folks to decide whether to treat this as a duplicate.

Note: See TracTickets for help on using tickets.