#63041 closed defect (bug) (fixed)
wp_get_canonical_url always returns false for attachments
Reported by: |
|
Owned by: |
|
---|---|---|---|
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
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
- Enable
wp_attachment_pages_enabled
in thewp_options
table (change 0 to 1) - Upload an image to Media > Add Media File
- Click on Edit and then click on the Media Permalink
- Check the source code for a canonical tag.
### Expected result
The canonical tag is in the code
### Results without the Patch
- 🐞 Bug occurs.
### Results with the Patch
- ✅ 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
@
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.
#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
. Theget_post_status()
function properly handles the 'inherit' status for attachments by checking their parent post's status.