If you use multilanguages, you will encounter the need to separate templates for different languages, namely HHeader and Footer (of course, they can be identical in appearance but different in content).
In this post, I will consider a quick solution to optimize and automate the separation and automatic substitution of the template in accordance with the current language version:
add_filter('render_block', function($block_content, $block) {
Filter: add_filter(‘render_block’ – not work for it…
add_filter('pre_render_block', function($pre_render, $parsed_block, $parent_block) {
if ($parsed_block['blockName'] === 'core/template-part') {
$slug = $parsed_block['attrs']['slug'] ?? '';
if (in_array($slug, ['header', 'footer']) && function_exists('pll_current_language')) {
$current_lang = pll_current_language();
// Check if language-specific version exists
$lang_template = locate_template("parts/{$slug}-{$current_lang}.html");
if ($lang_template) {
// Change slug attribute to language version
$parsed_block['attrs']['slug'] = "{$slug}-{$current_lang}";
// Render block with new attributes
return render_block($parsed_block);
}
}
}
return $pre_render;
}, 10, 3);
Filter: add_filter(‘pre_render_block’ – will be fine!
