Change Html lang attribute in WordPress
Would you like to change your html lang attribution in your multilingual website? Changing the html lang attribute is not difficult but it required some coding knowledge. If you using WordPress cms with less or no coding knowledge, then today I will teach you how to set custom language attribution in WordPress html.
If your website is a multilingual, or have two or more language used in your website then you might want google more easily to understand the language on your particular page. Lang attribute in your html is important to tell google bot that what language is about the page.
Maybe your website contain English content and Chinese content in certain page like Maomaochia.com. Then your English content page html code should mark a lang=âen-USâ or lang=âenâ; in your Chinese content page should be lang=âzhâ or lang=âzh-CNâ.
The âen-USâ or âenâ is the language code, (en) is mean for the language, the (US) is for Language localisation, mean target to specific country or region.
But default lang attribute in WordPress can be set in settings page. Like our Chinese content page, which lang attribute was set to en-US, which is not the best to tell google bot about this content language. Which it not good and not flexible to change the attribution for every page. In order to achieve the custom lang attribution function for each pages, we need to do it which some coding. But donât worry, I will show you exactly step by step to to do, just follow the instruction below.
In your themes header php, normally you can see the opening html have an <?php language_attributes(); ?> . This is the code to call your WordPress language you have selected.
To set language attribute for every single pages, we need to use a custom fields to set the attribution we want for every particular page. Which using the same tactic to my previous tutorial about change the protect page content.
How to change language in Html Code
So obviously the first step is you must have the custom fields supported in your WordPress site. If you donât have, you can simple install one for free.
After activate it, simple set a custom fields name. You can set whatever name you like, for me I use âpagelangâ as you can see. For the value, you can set it to the language code you like. For example, Chinese content page you can set the value to âzhâ, if you like to add in localisation, use [zh-(your country)]. Letâs said Malaysia, âzh-MYâ.
â ď¸ If you donât know how to set the custom fields, you can review my last post step 4.
Now, we need to replace the default value to this value in html lang attribute. The code below will replace the default lang code, so you need to set custom fields for every page, otherwise the code will be replaced empty.
add_filter('language_attributes', 'language_attributes_fix');
function language_attributes_fix( $language_attributes ) {
global $post;
return str_replace('en-US', get_post_meta($post->ID, 'pagelang', true) ,$language_attributes);
}
If you think this is not friendly, this code is better because it will automatically use the default attribution if you donât set a custom lang in particular page.
add_filter('language_attributes', 'language_attributes_fix');
function language_attributes_fix( $language_attributes ) {
global $post;
if(!empty($post->pagelang)){
return str_replace('en-US', get_post_meta($post->ID, 'pagelang', true) ,$language_attributes);}
return $language_attributes;
}
After you have successfully applied this code, you can see your page is not showing the right language attribute as what you wish to have.
How to use function code in WordPress?
Add these code to your functions.php file. If your themes have a child theme, you can just copy these code into your child themes functions.php and save it.
If you donât know how to add code in functions.php, you can use a plugins supported to do this. You can review this post there is a section called âAdding Custom JavaScript Code in WordPressâ I taught before how to add function code without knowledge skill.
The tutorial today is How you can set or change the Html lang attribute in your WordPress website. If you have any problem, please leave a comment below. Thank You