WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 3 months ago

#14458 closed enhancement (duplicate)

Create rel_canonical filter

Reported by: wjm Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Canonical Keywords: has-patch
Focuses: Cc:

Description

Wouldn't it be nice to be able to set the canonical rel value.
This comes in handy when you create a duplicate page and be able to manipulate the cannonical value.

Usage:

add_filter( 'rel_canonical', 'childtheme_rel_canonical', 10, 2 );
function childtheme_rel_canonical( $link, $id ) {
  return get_permalink( 55 );
}

Attachments (1)

rel_canonical_filter.patch (396 bytes) - added by wjm 4 years ago.

Download all attachments as: .zip

Change History (7)

wjm4 years ago

comment:1 nacin3 years ago

  • Milestone changed from Awaiting Review to Future Release

Not convinced this is necessary. Filters are already there for this in get_permalink(), and you could also unhook canonical and do it yourself for that page.

comment:2 husobj3 years ago

  • Cc ben@… added

@nacin

True, you could unhook canonical and do it yourself, but that isn't as robust as a filter.
Consider the following scenario.

If I use an SEO plugin, they will often unhook this and provide their own canonical implementation.

Now if I have a plugin installed that provides fairly complex functionality. For example, e-commerce, where you have products which would exist in multiple taxonomies under different URL paths. It is likely for such a plugin that it would want to override the canonical implementation to ensure that the most relevent URL is used as the canonical link for a product.

At this point the e-commerce plugin wouldn't necessarily know where an SEO had unhooked canonical and implemented it's own thing so you can't do anything about it. At this point it would be more helpful if canonical was a filter so that SEO plugins could filter it, then if another plugin wants to override if could apply a filter at a higher priority to override it.

Then say I create I create a plugin to add extra functionality to the e-commerce plugin and want to override their canonical functionality, I can override their filter.

I guess because the outcome of canonical should be a single value/output it should be a case of whichever plugin "shouts loudest wins" which is probably best achieve using a filter?

comment:3 here10 months ago

  • Cc mike@… added

see also #18660

comment:4 joostdevalk7 months ago

So, almost every theme and or plugin now unhooks this function and then adds their own function. This causes multiple canonicals to be on page every once in a while, so I'd *really* appreciate this filter :) (the patch still works other than the fact that the line number changed).

comment:5 joostdevalk7 months ago

Related to #18660 as well by the way.

comment:6 ocean903 months ago

  • Milestone Future Release deleted
  • Resolution set to duplicate
  • Status changed from new to closed

#18660 covers this as well.

Note: See TracTickets for help on using tickets.