We understand this is annoying. But without advertising-income, we can't keep making this site awesome for you.
Please disable your adblock and script blockers to view this page. Appreciate! 🙏
MaomaoChiaEnglish ArticlesHow to set Html lang attribute in WordPress

How to set Html lang attribute in WordPress

by MAL CHIA
how-to-set-html-lang-attribute-in-wordpress

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.

Change Html lang attribute in WordPress

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.

Html lang attribute

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.

Html lang attribute

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.

How to set Html lang attribute in WordPress

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 change language in Html Code

 

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

You may also like

Leave a Comment