Latin Slugs for Cyrillic, Greek and Other Scripts
Turns post and term titles written in Cyrillic, Greek, Georgian or other non-Latin scripts into readable Latin URL slugs (for example "Привет мир" becomes privet-mir) instead of long percent-encoded addresses.
Opens WPIDE on
Code
add_filter( 'sanitize_title', function ( $title, $raw_title, $context ) {
if ( 'save' !== $context || '' === $title || ! preg_match( '/[^\x00-\x7F]/', $title ) ) {
return $title;
}
static $table = null;
if ( class_exists( 'Transliterator' ) ) {
$tr = Transliterator::create( 'Any-Latin; Latin-ASCII; Lower()' );
if ( $tr ) {
$out = $tr->transliterate( $title );
if ( is_string( $out ) && '' !== $out ) {
return $out;
}
}
}
if ( null === $table ) {
$table = array(
'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'ґ' => 'g', 'д' => 'd', 'е' => 'e', 'є' => 'ye', 'ё' => 'yo', 'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'і' => 'i', 'ї' => 'yi', 'й' => 'y',
'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'ts', 'ч' => 'ch',
'ш' => 'sh', 'щ' => 'shch', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e', 'ю' => 'yu', 'я' => 'ya',
'α' => 'a', 'β' => 'v', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'i', 'θ' => 'th', 'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => 'x',
'ο' => 'o', 'π' => 'p', 'ρ' => 'r', 'σ' => 's', 'ς' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'ch', 'ψ' => 'ps', 'ω' => 'o',
'ά' => 'a', 'έ' => 'e', 'ή' => 'i', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ώ' => 'o',
);
}
return strtr( mb_strtolower( $title, 'UTF-8' ), $table );
}, 9, 3 );
Notes
Uses PHP's intl Transliterator when available and a built-in Cyrillic/Greek table otherwise. Existing slugs are not changed.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.