Make WordPress Core

Opened 2 months ago

Closed 4 weeks ago

Last modified 4 weeks ago

#63041 closed defect (bug) (fixed)

wp_get_canonical_url always returns false for attachments

Reported by: othernoel's profile othernoel Owned by: audrasjb's profile audrasjb
Milestone: 6.9 Priority: normal
Severity: normal Version: 6.4.3
Component: Canonical Keywords: has-patch has-unit-tests has-testing-info
Focuses: Cc:

Description

The code for wp_get_canonical_url makes this check

if ( 'publish' !== $post->post_status ) {
  return false;
}

When $post is an attachment, post_status is likely 'inherit' and never 'publish', meaning this check always fails.

Consequently, if attachments have their own pages, those pages don't get a rel="canonical" printed in wp_head, and the 'get_canonical_url' filter is never called for attachment pages. Is this intended behavior?

I'd assume get_post_status() to be the correct function here, as it resolves 'inherit' in attachments returning the parent post's status?

I haven't seen this documented anywhere, so I'm not sure if this way for backwards compatibility or an actual bug.

Change History (7)

This ticket was mentioned in PR #8435 on WordPress/wordpress-develop by @ankitkumarshah.


8 weeks ago
#1

  • Keywords has-patch added

#63041

## Description

This PR fixes an issue where wp_get_canonical_url() always returns false for attachments.

### Issue
Attachments have a post status of 'inherit' rather than 'publish'. The wp_get_canonical_url() function was directly checking $post->post_status === 'publish', causing it to always return false for attachments.

### Fix
This PR changes the condition to use get_post_status($post) instead of directly accessing $post->post_status. The get_post_status() function properly handles the 'inherit' status for attachments by checking their parent post's status.

#2 @SergeyBiryukov
5 weeks ago

  • Keywords needs-unit-tests added
  • Milestone changed from Awaiting Review to 6.9

This ticket was mentioned in PR #8571 on WordPress/wordpress-develop by @SirLouen.


5 weeks ago
#3

  • Keywords has-unit-tests added; needs-unit-tests removed

## Combined Test Report and Unit Tests Patch
### Description
This report validates that the indicated patch works as expected.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/8435.diff

### Environment

  • WordPress: 6.8-beta3-60042-src
  • PHP: 8.2.28
  • Server: nginx/1.27.4
  • Database: mysqli (Server: 8.4.4 / Client: mysqlnd 8.2.28)
  • Browser: Chrome 134.0.0.0
  • OS: Windows 10/11
  • Theme: My Twenty Twenty Child Theme 1.0
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.0

### Testing Instructions without the Patch

  1. Enable wp_attachment_pages_enabledin the wp_options table (change 0 to 1)
  2. Upload an image to Media > Add Media File
  3. Click on Edit and then click on the Media Permalink
  4. Check the source code for a canonical tag.

### Expected result
The canonical tag is in the code

### Results without the Patch

  1. 🐞 Bug occurs.

### Results with the Patch

  1. ✅ Issue resolved with patch.

### Additional Notes
I have also sorted all the PHPCS issues in the wpGetCanonicalUrl.php file (only missing comments)

Props to @Infinite-Null for the patch https://github.com/WordPress/wordpress-develop/pull/8435
Also Props to @othernoel who clearly hinted the patch solution in the report.

Trac ticket: https://core.trac.wordpress.org/ticket/63041

#4 @SirLouen
5 weeks ago

  • Keywords has-testing-info dev-feedback added

@SergeyBiryukov now this report is 100% complete and pretty simple and straightforward.
I think it can even make to 6.8.

#5 @audrasjb
4 weeks ago

  • Keywords dev-feedback removed
  • Owner set to audrasjb
  • Status changed from new to reviewing

It's too late for 6.8, we are at the RC step of the cycle.
Self assigning for final review and commit.

#6 @audrasjb
4 weeks ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 60108:

Canonical: Use get_post_status() to determine whether the post has a publish post status in wp_get_canonical_url().

This changeset fixes an issue where wp_get_canonical_url() always returned false for attachments. Attachments have a inherit post status rather than publish. The wp_get_canonical_url() function was directly checking $post->post_status === 'publish', causing it to always return false for attachments. Using get_post_status() fixes the issue as if the post is an attachment, then the parent post status will be given instead.

Follow-up to [37685].

Props othernoel, ankitkumarshah, SirLouen,
Fixes #63041.

#7 @audrasjb
4 weeks ago

In 60109:

Docs: Remove an unwanted trailing space found after [60108].

Follow-up to [60108].

See #63041, #63166.

Note: See TracTickets for help on using tickets.