[wp-trac] [WordPress Trac] #18660: Enhance rel_canonical function, add filter

WordPress Trac noreply at wordpress.org
Thu Jan 11 23:47:41 UTC 2018


#18660: Enhance rel_canonical function, add filter
-----------------------------------------+-----------------------------
 Reporter:  nathanrice                   |       Owner:  joostdevalk
     Type:  enhancement                  |      Status:  assigned
 Priority:  normal                       |   Milestone:  Future Release
Component:  Canonical                    |     Version:  3.3
 Severity:  normal                       |  Resolution:
 Keywords:  has-patch needs-testing SEO  |     Focuses:
-----------------------------------------+-----------------------------
Changes (by westonruter):

 * keywords:  has-patch needs-testing 2nd-opinion SEO => has-patch needs-
     testing SEO
 * milestone:  Awaiting Review => Future Release


Comment:

 I have a concern about `get_current_archive_link()` which appears in
 [attachment:canonical.6.patch], namely that it doesn't account for custom
 rewrite rules added by plugins. This is also a problem for
 `wp_get_canonical_url()` actually in regards to custom rewrite endpoints
 (anything other then `page` is stripped).

 What about an algorithm like the following to determine the default
 canonical URL for a given request:

 {{{#!php
 $added_query_vars = $wp->query_vars;
 if ( ! $wp_rewrite->permalink_structure || empty( $wp->request ) ) {
         $url = home_url( '/' );
 } else {
         $url = home_url( user_trailingslashit( $wp->request ) );
         parse_str( $wp->matched_query, $matched_query_vars );
         foreach ( $wp->query_vars as $key => $value ) {

                 // Remove query vars that were matched in the rewrite
 rules for the request.
                 if ( isset( $matched_query_vars[ $key ] ) ) {
                         unset( $added_query_vars[ $key ] );
                 }
         }
 }
 }}}

 This ensures that custom rewrite rules and endpoints are honored, as well
 as all public query vars.

 Note: I'm looking into this for the sake of adding canonical support to
 the [https://wordpress.org/plugins/amp AMP] plugin, as the AMP spec
 [https://www.ampproject.org/docs/reference/spec#canon requires] a
 `rel=canonical` link on every AMP response, even if it points to itself.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/18660#comment:35>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list