Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Language Attribute Variant Fix
*
* @package language-attributes-variant-fix
* @author Bernhard Kau
* @license GPLv3
*
* @wordpress-plugin
* Plugin Name: Language Attribute Variant Fix
* Plugin URI: https://kau-boys.de
* Description: Removes the language variant from the laguage attribute used on the HTML root element
* Version: 0.1
* Author: Bernhard Kau
* Author URI: https://kau-boys.de
* License: GPLv3
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*/
/**
* Strips the language variant
*
* @param string $language_attributes The original language attributes value.
*
* @return string
*/
function language_attributes_variant_fix( $language_attributes ) {
return preg_replace( '/lang="([\w]+-[\w]+)-[\w]+"/', 'lang="$1"', $language_attributes );
}
add_filter( 'language_attributes', 'language_attributes_variant_fix' );
@pixolin

This comment has been minimized.

pixolin commented Aug 14, 2018

function language_attributes_variant_fix( $language_attributes ) {
        return preg_replace( '/lang="([\w]+)-[\w]+[-\w]*"/', 'lang="$1"', $language_attributes );
}
add_filter( 'language_attributes', 'language_attributes_variant_fix' );

If we change the quantifier for the last segment, the function will work with de-DE als well as de-DE-formal.
I also selected the country code only, following the conversation on Twitter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment