Making WordPress.org


Ignore:
Timestamp:
11/26/2018 04:51:14 AM (6 years ago)
Author:
dd32
Message:

WordPress.org: hreflang: Add a default x-default hreflang tag and use the canonical URL for singular pages when possible.

Fixes #3947, #3948.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg/functions.php

    r7641 r7898  
    191191 */
    192192function hreflang_link_attributes() {
     193    // No hreflangs on 404 pages.
     194    if ( is_404() ) {
     195        return;
     196    }
     197
    193198    wp_cache_add_global_groups( array( 'locale-associations' ) );
    194199
     
    265270        );
    266271
     272        // Add x-default to the list of sites.
     273        $sites['x-default'] = (object) array(
     274            'locale'    => 'x-default',
     275            'hreflang'  => 'x-default',
     276            'subdomain' => '',
     277        );
     278
    267279        uasort( $sites, function( $a, $b ) {
    268280            return strcasecmp( $a->hreflang, $b->hreflang );
     
    270282
    271283        wp_cache_set( 'local-sites', $sites, 'locale-associations' );
     284    }
     285
     286    if ( is_singular() ) {
     287        $path = parse_url( get_permalink(), PHP_URL_PATH );
     288    } else {
     289        // WordPress doesn't have a good way to get the canonical version of non-singular urls.
     290        $path = $_SERVER['REQUEST_URI']; // phpcs:ignore
    272291    }
    273292
     
    276295            'https://%swordpress.org%s',
    277296            $site->subdomain ? "{$site->subdomain}." : '',
    278             $_SERVER['REQUEST_URI'] // phpcs:ignore
     297            $path
    279298        );
    280299
Note: See TracChangeset for help on using the changeset viewer.