# hreflang Tags for Regional Versions of Your Site

> Links matching pages across your country sites (such as example.com, example.co.uk and example.com.au) with hreflang tags, assuming each page uses the same path on every domain.

- Type: PHP
- Runs: Frontend only
- Category: [SEO](https://wpide.com/library/category/seo/)
- Tags: hreflang, international, multisite
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/seo-hreflang-regional-domains/

## Code

```php
// Language-region code => site address (no trailing slash). x-default is used for everyone else.
$wpide_regional_sites = array(
    'en-US'     => 'https://example.com',
    'en-GB'     => 'https://example.co.uk',
    'en-AU'     => 'https://example.com.au',
    'x-default' => 'https://example.com',
);

add_action( 'wp_head', function () use ( $wpide_regional_sites ) {
    if ( is_404() || is_search() || is_preview() || ( is_singular() && post_password_required() ) ) {
        return;
    }

    $path = (string) wp_parse_url( home_url( add_query_arg( array() ) ), PHP_URL_PATH );
    $base = (string) wp_parse_url( home_url( '/' ), PHP_URL_PATH );
    $path = '/' . ltrim( substr( $path, strlen( rtrim( $base, '/' ) ) ), '/' );

    foreach ( $wpide_regional_sites as $lang => $site ) {
        printf( '<link rel="alternate" hreflang="%1$s" href="%2$s">' . "\n", esc_attr( $lang ), esc_url( untrailingslashit( $site ) . $path ) );
    }
} );
```

## Notes

Add this to every regional site with the same map. Pages whose path differs between sites need a multilingual plugin instead.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "hreflang Tags for Regional Versions of Your Site" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
