A multilingual site with WordPress Multisite and hreflang
One way to run a site in two languages without a translation plugin is a Multisite network with one site per language: example.com/en/ and example.com/fr/, each with its own posts, menus and locale. Search engines then need hreflang links that connect each page to its translation, otherwise they treat the two versions as unrelated or as duplicates.
Problem: The English and French sites are separate sites in a network, Google shows the English page to French visitors, and the translations of a post are not linked.
Solution: Create a subdirectory network, set the language of each site in Settings, General, store for each post the ID of its translation on the other site in post meta, and print <link rel="alternate" hreflang> tags for the current page and all its translations in wp_head, using switch_to_blog() to get the URLs.
The network
// wp-config.php, above "That's all, stop editing!" define( 'WP_ALLOW_MULTISITE', true );
Then Tools, Network Setup: choose “Sub-directories” and copy the constants and the rewrite rules that WordPress shows into wp-config.php and .htaccess. The values are generated for your domain, so use them as shown rather than from a tutorial. After logging in again, create the sites /en/ and /fr/ in My Sites, Network Admin, Sites. Querying and switching between sites: Multisite get_sites() and switch_to_blog().
Link the translations
Each post stores a map “site ID to post ID” of all versions, including itself:
// e.g. on English post 12 (site 2), whose French version is post 34 on site 3: update_post_meta( 12, '_ha_translations', [ 2 => 12, 3 => 34 ] ); // and the same map on post 34 on site 3
In practice a small meta box with a “translation ID” field fills it; the map is saved on both sides.
The hreflang tags
// wp-content/mu-plugins/ha-hreflang.php
add_action( 'wp_head', function () {
if ( ! is_multisite() || ! is_singular() ) {
return;
}
$map = get_post_meta( get_queried_object_id(), '_ha_translations', true );
if ( ! is_array( $map ) || count( $map ) < 2 ) {
return;
}
foreach ( $map as $blog_id => $post_id ) {
switch_to_blog( (int) $blog_id );
$post = get_post( (int) $post_id );
if ( $post && 'publish' === $post->post_status ) {
$lang = str_replace( '_', '-', get_locale() ); // en-US, fr-FR
printf( '<link rel="alternate" hreflang="%s" href="%s" />' . "\n", esc_attr( $lang ), esc_url( get_permalink( $post ) ) );
}
restore_current_blog();
}
printf( '<link rel="alternate" hreflang="x-default" href="%s" />' . "\n", esc_url( get_home_url( get_main_site_id(), '/' ) ) );
} );
get_locale() returns the language of the switched site, because the language is a site option (WPLANG). The list includes the current page itself, as Google requires, and every version must carry the same set of links; a one-sided link is ignored. x-default is the page for visitors whose language matches none of the versions; here it is the main site’s home page, a language chooser page is the usual alternative.
For the home pages and archives, add the same tags with the home URLs of all language sites.
Check
curl -s https://example.com/en/hello-world/ | grep hreflang
Check a few pages of each language with an hreflang testing tool: the typical errors are missing return links and wrong language codes (en_US with an underscore is invalid, it must be en-US).
NOTE: Language-specific content belongs on the language site: the sitemap of each site (/en/wp-sitemap.xml) lists only its own posts, which is what search engines expect. Keep the slugs translated as well; hreflang connects different URLs, it does not need them to match.