-1

How can you retrieve the language of a WordPress website, without the added region suffix attached to it? In other words, just get the language, without the entire locale.

E.g. <html lang="en"> instead of <html lang="en-US">

I saw a related question but it doesn't provide the exact solution, both of these are same:

  • <html <?php language_attributes();?>>
  • <html lang="<?php bloginfo("language"); ?>">

The reason I'm wondering this is for theming purposes, because I have noticed (after much testing) negative SEO impact on some websites that specify the region instead of just the language. Google has claimed repeatedly that this shouldn't be the case, and that they practically ignore the <html lang="en-US"> type of flags, but I have concluded this simply isn't true after reviewing lots of SERPs from various global locations, and would like to allow our WordPress theme users to simply have the language specified without the region.

I know this can be done with some PHP magic, but I'm wondering if any existing function?

8
  • Related: wordpress.stackexchange.com/questions/119495/… Commented Nov 8, 2023 at 8:14
  • Just so you're aware, this is not something a theme has any business modifying, and doing something like this would almost certainly not be allowed by the theme repository. This type of thing, regardless of its effectiveness, would belong in a plugin and be done using a filter, such as language_attributes or bloginfo, rather than modifying the template file. Commented Nov 8, 2023 at 8:23
  • @JacobPeattie I appreciate your opinion, but I disagree, and I also don't care about WordPress.org or Automattic, as I'm banned from their community. Commented Nov 8, 2023 at 8:40
  • This is ill advised since the "region" has realworld consequences, e.g. spanish in Mexico is not the same as spanish in Spain and requires real changes to be made. There are also a lot of language codes that don't have 2 letter variants, or for which the regions indicate completely different language. Commented Nov 9, 2023 at 8:11
  • Hmm I see what you mean @TomJNowell seems WP Core doesn't follow ISO standards. It would probably require some PHP conversion to ISO 2-letter standards regardless in that case. Commented Nov 12, 2023 at 5:17

1 Answer 1

0

I'm posting this at the request of one of our Discord users, who doesn't use WPSE:

Stack Overflow / Stack Exchange is kind of a bizarre place. Why is that question voted "-2"? The question is basically asking more or less the same thing that multiple WordPress tickets are asking for:

https://core.trac.wordpress.org/ticket/48152

https://core.trac.wordpress.org/ticket/50183

Also the W3C itself seems to recommend the same thing (avoiding the region in most cases):

https://www.w3.org/International/articles/language-tags/

https://www.w3.org/International/questions/qa-choosing-language-tags

But somehow the question offended the WordPress techbros, so it got voted -2

To answer the question: WordPress does sort of have a way of getting this, but it's awfully obscure:

https://api.wordpress.org/translations/core/1.0/

If you pretty-print that, the entries look like this:

{ "language": "en_GB", "version": "6.4.1", "updated": "2023-11-24 12:26:36", "english_name": "English (UK)", "native_name": "English (UK)", "package": "https://downloads.wordpress.org/translation/core/6.4.1/en_GB.zip", "iso": { "1": "en", "2": "eng", "3": "eng" }, "strings": { "continue": "Continue" } }, 

The "iso" property contains the 2-letter and/or 3-letter language codes, without any region code. (I'm not sure why "eng" is there twice.) WordPress itself uses this for the lang="..." attribute in the language dropdown on the page wp-admin/options-general.php Also "en_US" is a special case (since it has no translation) and is just hard-coded as lang="en":

https://github.com/WordPress/WordPress/blob/6.4.1/wp-includes/l10n.php#L1616

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.