0

I'm looking for option to change language_attributes manually without hardcoding it like

html lang="en-US"

in html it self. The reason for this is I have three language site, using same WP engine and I want one and each of them run with specific lang attribute. Is this possible? It would be nice to have something like this to change it:

<?php language_attributes('lang="en-US"'); ?>
2

You can use a filter to modify the value before it is set

function __language_attributes($lang){

  // ignore the supplied argument
  $langs = array( 'en-US', 'KO', 'JA' );

  // change to whatever you want
  $my_language = $langs[0];

  // return the new attribute
  return 'lang="'.$my_language.'"';
}

add_filter('language_attributes', '__language_attributes');

Then just make sure your theme header has the correct php function

<!DOCTYPE html>
<html <?php language_attributes(); ?> class="no-js">
<head>

Another approach would be to set the language in a global variable before the filter is called.

// Make this variable global
global $__language_attribute;

// Set the language we want to use
$__language_attribute = 'en-US'; 

// Listen for the language filter
add_filter('language_attributes', '__language_attributes_use_global');

function __language_attributes_use_global($lang){
   global $__language_attribute;
   return "lang=\"$__language_attribute\"";
}

ALTERNATE

// set your language here
$my_lang = 'KO';

// subscribe with closure to apply this value later
add_filter('language_attributes', function($lang) use ($my_lang) {
    return 'lang="' . $my_lang . '"'; 
});
  • Thank you very much, Jgraup! I just want to ask, how can I define what array number to use in the language_attributes function in HTML? Because if I'll change $langs to other number it will define that language to all pages – hunnt Nov 27 '15 at 19:59
  • i was just showing an example that you could select from an array of languages. What do you do to determine which language you want to use for that page? – jgraup Nov 27 '15 at 22:41
  • I thought to simply input the right code to the header. Like language_attribute and something in the brackets to define what language I want to choose. For example: language_attributes(english); or something like that. Is this possible? – hunnt Nov 27 '15 at 23:27
  • I'm sure you have an idea no how you would call the function but I'm not personally clear. If you give me a little more context then I could help you with your specific need. Do you mean, if you type English, then your content will magically show up as English, and the same for all languages? If you want to make life easier for yourself, wordpress.org/plugins/polylang is a great plugin for multiple languages. But if you have more code I could take another look. – jgraup Nov 28 '15 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.