Make WordPress Core

Opened 2 years ago

Closed 8 months ago

#52324 closed defect (bug) (duplicate)

Sample permalinks unavailable for attachment pages.

Reported by: peterwilsoncc's profile peterwilsoncc Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Media Keywords: close
Focuses: Cc:

Description

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 (4)

#1 @Mista-Flo
2 years ago

Hey,

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:

<?php
        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
2 years 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.

This ticket was mentioned in Slack in #core-media by antpb. View the logs.


8 months ago

#4 @antpb
8 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #36976.

I agree this can be closed as duplicate.

Note: See TracTickets for help on using tickets.