Make WordPress Core

Opened 9 years ago

Closed 8 years ago

#36168 closed enhancement (fixed)

Introduce get_canonical_url function

Reported by: joostdevalk's profile joostdevalk Owned by: ocean90's profile ocean90
Milestone: 4.6 Priority: normal
Severity: normal Version:
Component: Canonical Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

Short version

We need a function that allows consistent retrieving and filtering of the canonical URL. I'm proposing creating a get_canonical_url function to do just that.

Explanation

With the advent of more and more different ways to deliver content, we need to make sure the canonical URL on all these different manifestations of content is correct. Right now, all the plugins out there, for instance the AMP plugin here, the the Instant Articles plugin here and core's own post oEmbed functionality, all in some way use get_permalink to get the canonical. The point of a canonical URL is that it can point to the canonical version and that this does not necessarily have to be the representation in the WP database. Thus you need to be able to filter it.

I'm attaching a patch that introduces get_canonical_url and uses this in core's rel_canonical function, which is used on both the normal output of posts and pages as well as on oEmbed iframes.

Attachments (4)

get_canonical.diff (2.2 KB) - added by joostdevalk 9 years ago.
First patch
36168-get_canonical-2.diff (6.3 KB) - added by jipmoors 9 years ago.
Fixed apply_filters and added tests
36168.patch (5.6 KB) - added by ocean90 8 years ago.
36168.diff (5.7 KB) - added by DrewAPicture 8 years ago.

Download all attachments as: .zip

Change History (20)

@joostdevalk
9 years ago

First patch

#1 @joostdevalk
9 years ago

  • Owner set to joostdevalk
  • Status changed from new to assigned
  • Summary changed from Introduce get_canonical function to Introduce get_canonical_url function

#2 @SergeyBiryukov
9 years ago

  • Keywords 4.6-early added
  • Milestone changed from Awaiting Review to Future Release

#3 @bjornjohansen
9 years ago

There are quite a few plugins out there that modify the permalink, e.g. analytics-plugins that add tracking codes to the URL to let publishers know that the visitor came via a feed.

But in some instances, like with the Instant Articles plugin mentioned in the ticket explanation, it is critical to get a clean canonical URL. A get_canonical_url() would be very useful, so we can be assured that we provide the actual canonical URL and not some permalink that has been tampered with.

I’m very much +1 on this.

#4 @swissspidy
9 years ago

  • Keywords needs-unit-tests added

#5 follow-up: @markjaquith
9 years ago

So the main difference is that this integrates post paging and comments paging?

#6 in reply to: ↑ 5 @joostdevalk
9 years ago

Replying to markjaquith:

So the main difference is that this integrates post paging and comments paging?

No, that code was there already, just got moved as otherwise this wouldn't work. I'd say the main difference is that we have a specific function and filter for the canonical, because it's conceptually different from the rather generic get_permalink.

#7 follow-up: @joemcgill
9 years ago

This may also be a good place to enforce a canonical scheme for sites. Related: #30581

#8 in reply to: ↑ 7 @joostdevalk
9 years ago

Replying to joemcgill:

This may also be a good place to enforce a canonical scheme for sites. Related: #30581

If that gets dev approval I'll happily code a patch.

@jipmoors
9 years ago

Fixed apply_filters and added tests

#9 @jipmoors
9 years ago

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

Bit unsure what name to give the test file, so if there is a better name or place for it please let me know.

#10 @SergeyBiryukov
9 years ago

  • Milestone changed from Future Release to 4.6

This ticket was mentioned in Slack in #core by chriscct7. View the logs.


8 years ago

#12 @ocean90
8 years ago

  • Keywords dev-feedback 4.6-early removed
  • Owner changed from joostdevalk to ocean90
  • Status changed from assigned to reviewing

This ticket was mentioned in Slack in #core by voldemortensen. View the logs.


8 years ago

@ocean90
8 years ago

#14 @ocean90
8 years ago

@joostdevalk, @jipmoors: Thanks for your patches. 36168.patch is a refresh and includes following changes:

  • Name of function should start with wp_: wp_get_canonical_url()
  • Removes the default argument for get_permalink()
  • Docs tweaks
  • Tests: Use shared test fixture
  • Tests: Use WP_UnitTestCase::go_to() and WP_UnitTestCase::set_permalink_structure()
  • Tests: Move test class to the /link/ directory

Let me know what you think.

@DrewAPicture
8 years ago

#15 @DrewAPicture
8 years ago

36168.diff improves 36168.patch by adding a changelog entry to the DocBlock for rel_canonical() and other minor docs tweaks.

#16 @ocean90
8 years ago

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

In 37685:

Canonical: Introduce wp_get_canonical_url().

wp_get_canonical_url() encapsulates the URL logic of rel_canonical() to provide a consistent way to retrieve the canonical URL for a post.
The new filter get_canonical_url allows to customize the canonical URL.

Props joostdevalk, jipmoors, DrewAPicture, ocean90.
Fixes #36168.

Note: See TracTickets for help on using tickets.