How to Localize WordPress Navigation Menus and Footer Widgets Programmatically
Localize classic WordPress navigation and footer widgets by mapping the active request language to a registered theme_location and a sidebar ID. This works for classic themes and hybrid themes that still call PHP template functions; block themes use Navigation and Template Part blocks instead, so classic menu assignments won’t change what those blocks render.
Keep the routing in code and the content in WordPress. Editors can then change link labels, reorder items, or update footer copy without a deployment. The production caveat is fallback: an unassigned menu location must not put the default-language menu on a translated page, and the page cache must not serve markup rendered for another language.
Choose the Right WordPress Surface
| What you’re localizing | Runtime selector | What editors maintain |
|---|---|---|
| Classic navigation | wp_nav_menu() with theme_location | A separate menu assigned to each registered location |
| Classic footer widgets | dynamic_sidebar() with a registered sidebar ID | Widgets in each language’s footer area, including block widgets where supported |
| Block navigation | core/navigation with a ref to a saved navigation entity, or inline navigation blocks | Navigation in the Site Editor |
| Block footer | core/template-part with a slug | A footer template part for each language |
Inspect the markup source in your theme, not just the dashboard. A hybrid theme can use wp_nav_menu() in its header and a block template part in its footer. The two surfaces can share a language resolver without sharing a rendering method.
Resolve Language When the Template Renders
Read the language selected for the current request, not the site’s default language or the administrator’s profile locale. Put the resolver and classic registrations in wp-content/mu-plugins/site-chrome-locales.php. This example supports English, French, and Arabic with one menu and sidebar per base language. The resolver returns an empty string for languages that have no configured route.
<?php
function pubchrome_languages(): array {
return [
'en' => ['name' => 'English', 'nav' => 'Main navigation'],
'fr' => ['name' => 'French', 'nav' => 'Navigation principale'],
'ar' => ['name' => 'Arabic', 'nav' => 'التنقل الرئيسي'],
];
}
function pubchrome_language(): string {
$active = function_exists('pll_current_language')
? pll_current_language('slug')
: apply_filters('wpml_current_language', null);
if (!is_string($active) || $active === '') {
$active = get_locale();
}
$normalized = strtolower(str_replace('_', '-', $active));
$routes = [
'en' => 'en', 'en-us' => 'en', 'en-gb' => 'en',
'fr' => 'fr', 'fr-fr' => 'fr', 'fr-ca' => 'fr',
'ar' => 'ar', 'ar-sa' => 'ar',
];
return $routes[$normalized] ?? '';
}
add_action('after_setup_theme', static function (): void {
$locations = [];
foreach (pubchrome_languages() as $key => $labels) {
$locations['chrome-primary-' . $key] = 'Primary — ' . $labels['name'];
}
register_nav_menus($locations);
}, 20);
add_action('widgets_init', static function (): void {
foreach (pubchrome_languages() as $key => $labels) {
register_sidebar([
'name' => 'Footer — ' . $labels['name'],
'id' => 'chrome-footer-' . $key,
'before_widget' => '<div id="%1$s" class="footer-widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h2 class="footer-widget-title">',
'after_title' => '</h2>',
]);
}
});
Registration creates editor-facing slots; it doesn’t create or assign any content. All slots are registered on every request so you can edit them regardless of the dashboard language. The explicit alias list lets you share a menu between fr-FR and fr-CA now and split them later without accidentally accepting a locale you haven’t configured.
| Language source | API | What to verify |
|---|---|---|
| Polylang | pll_current_language('slug') | Map the configured slug, which may not match the WordPress locale. |
| WPML | apply_filters('wpml_current_language', null) | Check the result on a translated frontend URL, after language resolution. |
| WordPress locale | get_locale() | Treat this as a fallback, not as a multilingual URL router. Single-language installations generally return the same site locale on every public URL. |
Enable one multilingual routing integration, not both. If yours has its own menu-location translation feature, choose whether that feature or your PHP mapping owns assignments. On staging, compare the assigned menu with the rendered menu: language plugins can filter menu objects, menu items, and destination URLs after your code selects a location.
Never translate a routing identifier in an editor or translation file. The display name can change;
chrome-primary-fr,chrome-footer-fr, and the template-part slugs must stay stable.
Provision Menus and Footer Areas
Create a menu for each supported language and assign it to the matching location. For WP-CLI provisioning, set EN_HOME_ID, FR_HOME_ID, and AR_HOME_ID to the actual translated page IDs before running these commands. Menu creation is for a new site; on an existing site, inspect wp menu list and reuse the existing menu slugs or IDs.
wp menu create 'Main EN'
wp menu create 'Main FR'
wp menu create 'Main AR'
wp menu item add-post main-en "$EN_HOME_ID"
wp menu item add-post main-fr "$FR_HOME_ID"
wp menu item add-post main-ar "$AR_HOME_ID"
wp menu location assign main-en chrome-primary-en
wp menu location assign main-fr chrome-primary-fr
wp menu location assign main-ar chrome-primary-ar
wp menu location list
wp sidebar list --fields=name,id
wp widget list chrome-footer-fr
Check the slugs returned by WordPress before running the assignment commands; collisions can cause a suffix, and blindly rerunning creation can produce duplicates. Location assignments belong to the active theme on the current site, so verify them again after a theme switch or a multisite deployment. Add footer content under Appearance → Widgets and confirm each area contains its own widget instances before treating the CLI inventory as complete.
Translate link destinations as well as labels. If the French label points to the English page, rebuild the item from the French page or use the multilingual plugin’s supported menu synchronization, then verify the rendered href. For footer terms, privacy links, and consent text, use the approved destination for that locale rather than copying the default URL.
Render Classic Menus and Widgets Without Silent Fallback
Start by finding calls that assume a single language. This pair looks reasonable on a monolingual installation but doesn’t select content based on the request:
<?php
wp_nav_menu(['menu' => 'Main EN']);
dynamic_sidebar('chrome-footer-en');
Both calls hard-code English content. A renamed or missing menu can also trigger WordPress menu fallback, so the result may still look like a working navigation bar. Replace these calls rather than adding localized output alongside them.
<?php
// In the header, where the primary navigation belongs:
$language = pubchrome_language();
$location = 'chrome-primary-' . $language;
if ($language !== '' && has_nav_menu($location)) {
$labels = pubchrome_languages();
$menu = wp_nav_menu([
'theme_location' => $location,
'container' => false,
'fallback_cb' => false,
'menu_id' => 'primary-menu-' . $language,
'menu_class' => 'primary-menu',
'echo' => false,
]);
if (is_string($menu) && trim($menu) !== '') {
printf(
'<nav aria-label="%s">%s</nav>',
esc_attr($labels[$language]['nav']),
$menu
);
}
}
// In the footer, inside the existing site footer:
$language = pubchrome_language();
$sidebar = 'chrome-footer-' . $language;
if ($language !== '' && is_active_sidebar($sidebar)) {
printf('<div id="%s" class="footer-widgets">', esc_attr($sidebar));
dynamic_sidebar($sidebar);
echo '</div>';
}
The location check and fallback_cb prevent an unassigned menu from turning into unrelated page links or another menu. The sidebar check avoids the wrapper for an inactive area, although individual widgets can still choose not to output on a given request. Inspect the rendered DOM for a named nav landmark, unique IDs, and valid list markup; keep the generated menu HTML intact rather than escaping the whole string.
Never use the default-language footer as an implicit fallback for legal or privacy links. If missing content would make the page unusable or noncompliant, require a complete localized footer before publishing that locale.
Route Block Theme Parts Separately
For a block theme, use a distinct header or footer template part per language. The parent template can reference a default-language part such as footer-en; that also gives editors a meaningful default in the Site Editor. Create the French and Arabic parts first, for example as parts/footer-fr.html and parts/footer-ar.html in the active theme, or as theme-scoped template parts saved in WordPress. Use the same pattern for headers, and select a different Navigation entity inside each header part.
add_filter('render_block_data', static function (array $block): array {
if (is_admin() || (defined('REST_REQUEST') && REST_REQUEST)) {
return $block;
}
if (($block['blockName'] ?? '') !== 'core/template-part') {
return $block;
}
$slug = $block['attrs']['slug'] ?? '';
$part_theme = $block['attrs']['theme'] ?? get_stylesheet();
if ($part_theme !== get_stylesheet()
|| !in_array($slug, ['header-en', 'footer-en'], true)) {
return $block;
}
$language = pubchrome_language();
if ($language === '') {
return $block;
}
$part = $slug === 'header-en' ? 'header' : 'footer';
$block['attrs']['slug'] = $part . '-' . $language;
return $block;
});
Add this filter inside the PHP file after the resolver, and use it only on a block surface. It redirects the named parts on public pages without rewriting saved block markup; editor and REST previews continue to show the stored part. An unsupported locale keeps that original part in this example, so only enable public locales with configured parts if mixed-language chrome is unacceptable.
A Navigation block’s ref is a database ID, not a portable slug. When moving a theme between sites, remap those IDs or select the correct saved navigation in each destination’s Site Editor. Also check for database-saved template-part edits: they can override a theme file with the same slug, so deploying a changed parts/footer-fr.html may not change the page.
Verify the Rendered Page and the Cache
For the classic example, probe the public HTML and look for the selected IDs and document language. Set BASE to the public base URL without a trailing slash, and replace the paths with actual routes. Include a deep translated URL as well as the language homepage.
for path in / /fr/ /ar/ "$FR_DEEP_PATH"; do
printf '\n%s\n' "$path"
curl -fsSL "${BASE}${path}" |
grep -Eo '<html[^>]*>|id="primary-menu-[^"]+"|id="chrome-footer-[^"]+"'
done
Expect the menu and footer IDs to agree with the request language, and check that the Arabic document carries dir="rtl" when the locale is configured as RTL. This probe is a quick inspection, not an assertion suite: a redirect followed by curl can hide a bad route, so inspect the final URL and actual links if anything differs. For block parts, check distinctive links or add stable markers to the localized parts instead of looking for the classic IDs.
Use HTML Translate to compare visible labels in exported HTML snapshots, then check destinations and accessibility names on the live pages. Repeat the live checks with a warm anonymous cache, alternating languages on the same device. Path- or host-based language routing should preserve that distinction at the CDN and page-cache layers; if the same URL varies by cookie, configure explicit cache variation or bypass caching for that response. Confirm that editing one menu or footer purges every public page that embeds it.
Common Mistakes and Troubleshooting
- The French URL shows English links. Check the resolved language at render time, verify the
chrome-primary-frassignment, then compare a cold response with a cached one. - A list of pages appears where the menu should be. A missing menu triggered default behavior. Check
has_nav_menu()and pass'fallback_cb' => false. - The footer area is listed but displays nothing. Verify widget instances with
wp widget list chrome-footer-fr, then check widget visibility rules on the affected public URL. - Editing a footer file has no effect. Check for a saved
wp_template_partoverride and confirm the selected part belongs to the active theme. - Menu labels are translated but clicks switch languages. Inspect each rendered
hrefand correct the menu item’s target page or custom URL. - Arabic renders with left-to-right spacing. Verify document direction via
language_attributes()in the classic theme, and replace directional spacing such asmargin-leftwith logical properties such asmargin-inline-startwhere appropriate.
WordPress Navigation Menus Footer Questions
Can two regions share one menu?
Yes. Map fr-FR and fr-CA to the same fr route when the destinations and labels match. Give them separate keys if regional navigation or legal content differs.
Does translating the theme translate menu items?
No. Theme translation functions such as __() handle strings looked up in a text domain. Menu labels and widget content are database content and need their own editorial workflow or multilingual plugin integration.
Can a widget area contain localized blocks?
Yes. On a classic theme with the block-based Widgets screen, editors can place blocks in each registered area. The template still selects the area with dynamic_sidebar(); it doesn’t need to know which widget or block types it contains.
Should the language switcher be a group of custom menu links?
Not if users should stay on the translated equivalent of the current page. Use the multilingual integration’s switcher so it selects related content and applies its policy when no translation exists.
Can the same WordPress menu serve desktop and mobile?
Yes. Render the same theme_location and use a responsive disclosure when practical. If the theme emits two menu trees, give them distinct menu_id values, check descendant IDs too, and keep the inactive tree out of keyboard navigation.
Does localized navigation generate hreflang?
No. Menu routing controls visible links. Page-level hreflang annotations must come from the multilingual integration or SEO layer and point to the corresponding canonical language versions.