Guide

How to add a language switcher to WordPress

A WordPress language switcher is a short list of links that takes a visitor from the page they are on to the same page in another language. WordPress does not include one, so a multilingual plugin adds it, usually as a shortcode, a block, a widget or a menu item. With Vabelon it is one shortcode: [vabelon_switcher].

What a good language switcher does

  • It links to the same page in the other language, not to the home page. A visitor reading your prices in English wants your prices in Spanish.
  • It names each language in that language. The W3C’s guidance on links to localized content gives the example: the link should read “français”, not “French”. As a result, a visitor who does not read English can still find their language.
  • It leaves the choice to the visitor. Google advises against sending visitors to a language automatically, and suggests links to the other language versions of a page instead.
  • It marks the current language, for people who see the page and for people who hear it.

Where a language switcher belongs

Put your WordPress language switcher where visitors expect it and where it appears on every page: the header, or the footer. The header suits a site whose visitors often arrive in the wrong language. The footer suits a site with a full header and an audience that mostly reads one language.

Add a language switcher with a shortcode

Add the shortcode wherever the links should appear:

[vabelon_switcher]

It works in a page, in a widget and in a Shortcode block. To show it on every page, place a Shortcode block in the header or footer of your theme, in the site editor or in a widget area.

Add a language switcher in a theme template

If you build or edit themes, call the template tag where the switcher should print, for example in header.php:

<?php vabelon_switcher(); ?>

In both cases, the output is the same markup.

What the switcher shows

  • On a page or post, it links to the published translations of that page. A page with no translations shows no switcher, so no link ever leads to a missing page.
  • Everywhere else, such as the blog index, it links to the home address of each language.
  • Each language appears under its own name, such as Español, and the current one is marked.

However, the links only appear once you connect the pages. See link translations.

Style the language switcher with CSS

Vabelon loads no stylesheet, so the switcher takes the fonts and colors of your theme. It is a plain list inside a nav element, with these classes:

.vabelon-switcher
.vabelon-switcher__list
.vabelon-switcher__item
.vabelon-switcher__link
.vabelon-switcher__link--current

A horizontal switcher with the current language in bold:

.vabelon-switcher__list {
	display: flex;
	gap: 1rem;
	list-style: none;
	margin: 0;
	padding: 0;
}
.vabelon-switcher__link--current {
	font-weight: 600;
}

Languages separated by a line, a common footer style:

.vabelon-switcher__item + .vabelon-switcher__item {
	border-left: 1px solid currentColor;
	padding-left: 1rem;
}

Add the CSS under Appearance, then Customize, then Additional CSS, or in your theme’s stylesheet. The full markup is in the language switcher and hooks and CSS classes.

Built for screen readers

The switcher is a nav element labelled “Languages”. The current language carries aria-current="page", and each language name is marked with its own lang, so a screen reader pronounces “Español” as Spanish. That last detail is what the HTML lang attribute is for.

Built by