Fixing incorrect og:locale in WordPress SEO Yoast Plugin and Genesis

I noticed that the famous WordPress plugin for SEO by Yoast does not allow a flexible handling of post languages, expecially on this website we’re i’ve mixed language.

Hopefully its easy to fix expecially when using the Genesis theme framework.

In functions.php of your own child theme put this code at the the bottom:

add_filter('wpseo_locale', 'my_wpseo_locale' );

function my_wpseo_locale() {
    $locale = genesis_get_custom_field('locale');
    if (strlen($locale)>=2) return $locale;
    return 'it_IT';

What this code do is looking for a custom field called “locale” that you can add to any posts to override the default one.

I choosed it_IT as default just for example but you can do it your own way.

If you’re not using the Genesis framework you need to implement yourself the genesis_get_custom_field function that retrieve the custom field value from the current post or page.

Extend the changes to other plugins

Filtering by hooking the wpseo_locale hook will only affect the Yoast plugin.

If you want to extends those changes to other plugins, for instance the Genesis builtin “eNews” plugin, you have to manipulate the return value of the get_locale() wordpress core function by hooking the locale hook.

You can do this by just replacing:

add_filter('wpseo_locale', 'my_wpseo_locale' );


add_filter('locale', 'my_wpseo_locale' );
Fixing incorrect og:locale in WordPress SEO Yoast Plugin and Genesis, 5.0 out of 5 based on 1 rating

Potrebbe interessarti anche:

Iscriviti alla newsletter

  • HowTo e Tutorial su Linux e Ubuntu
  • Wordpress, Perl & Webmin
  • VoIP, Asterisk, SIP

Antonio Gallo is a Linux entusiast and evangelists since 1996 when he started the Bad Penguin project. Notably he was in the board and sysadmin of for many years and also managed the "Linux Day" event in Italy, wrotes for technical magazines. Antonio writes, codes and develops Linux and Open Source software solutions. When everybody else fails he's the man to be called: bash, awk, php, perl, C are the rabbits in his cylinder ready to spread the Linux magic around the world.