DEV Community

Cover image for Conditional schema setup with Yoast & WP
SeanAUS120
SeanAUS120

Posted on • Updated on

Conditional schema setup with Yoast & WP

Wordpress and multilanguage plugins are a horrifying experience all round. WPML is one of the slowest, resource intensive plugins I've seen.

Every now and again I want a few language specific pages for SEO in other countries. So I prefer to do it without any other plugins and here's how I did it on my presets landing page.

Set the language in header.php

First you need to set the language for that page in your header like:

<?php
if ( is_single( 'german' ))  {  
echo '<html lang=de-DE class="no-js">'; 
}
elseif ( is_single( 'poland' ))  {  
echo '<html lang=pl-PL class="no-js">'; 
}
else                           
{  
echo '<html lang=en-US class="no-js">'; 
} ?>
Enter fullscreen mode Exit fullscreen mode

Override Yoast's schema in functions.php

Now here's the important part. If you use an SEO plugin (you should) like Yoast, it puts in schema that your language is the main site language, and doesn't look for your custom code in header.php.

So we need to override Yoast's schema for specific pages like this:

function change_inLanguage( $data ) { 
  if ( is_single( 'german' ) ) {  $data['inLanguage'] = 'de-DE';   }
  else if ( is_single( 'polish' ) ) {  $data['inLanguage'] = 'pl-PL';   }
  else {  $data['inLanguage'] = 'en-US';  }
  return $data;   
}
add_filter( 'wpseo_schema_webpage', 'change_inLanguage' );
Enter fullscreen mode Exit fullscreen mode

Now you can check on Google Structured Data Tool that it's working and Google will do the rest.

See it in action here:

EN
FR
DE
RU.
WC
XX
X

Top comments (0)