Greeklish permalinks in WordPress without a plugin
Give new Greek posts, pages and categories addresses in Latin letters with a Snipfire snippet, plus one for file names. Here is how they compare with AutoConvert Greeklish Permalinks.
What AutoConvert Greeklish Permalinks does
AutoConvert Greeklish Permalinks turns Greek letters in post and term addresses into Latin letters, and can convert old addresses too. The plugin on WordPress.org
What the snippet does the same
- New posts, pages, products, categories and tags with Greek titles get addresses in Latin letters.
- You don't have to edit the address by hand.
What's different
- The plugin can convert all your existing addresses with one click. The snippet only changes new ones, and existing addresses stay as they are.
- The plugin lets you choose how letter pairs like αι, ει and ευ are written, and which post types and taxonomies to convert. The snippet converts letter by letter (only ου becomes ou) for everything.
- The second snippet also gives uploaded files with Greek names Latin names. The plugin works on addresses only.
When to keep the plugin Keep the plugin if you need to convert the addresses of existing content or want control over how letter pairs are written.
The snippet: Greeklish addresses for Greek titles
New posts, pages, products, categories and tags with Greek titles get addresses in Latin letters: “Καλοκαιρινά φορέματα” becomes kalokairina-foremata. Existing addresses don’t change.
<?php
$greeklish = static function ( string $text ): string {
$text = mb_strtolower( $text, 'UTF-8' );
$text = strtr( $text, array( 'ου' => 'ou', 'ού' => 'ou' ) );
return strtr(
$text,
array(
'α' => 'a', 'ά' => 'a', 'β' => 'v', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'έ' => 'e',
'ζ' => 'z', 'η' => 'i', 'ή' => 'i', 'θ' => 'th', 'ι' => 'i', 'ί' => 'i', 'ϊ' => 'i',
'ΐ' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => 'x', 'ο' => 'o',
'ό' => 'o', 'π' => 'p', 'ρ' => 'r', 'σ' => 's', 'ς' => 's', 'τ' => 't', 'υ' => 'y',
'ύ' => 'y', 'ϋ' => 'y', 'ΰ' => 'y', 'φ' => 'f', 'χ' => 'ch', 'ψ' => 'ps', 'ω' => 'o',
'ώ' => 'o',
)
);
};
add_filter(
'sanitize_title',
static function ( $title, $raw_title = '', $context = 'display' ) use ( $greeklish ) {
if ( 'save' === $context && preg_match( '/\p{Greek}/u', (string) $title ) ) {
return $greeklish( (string) $title );
}
return $title;
},
5,
3
);
Notes, where it runs and what it was tested with
The snippet: Greeklish file names for uploads
Uploaded files with Greek names get names in Latin letters (φωτογραφία.jpg becomes fotografia.jpg), so their addresses work everywhere.
<?php
$greeklish = static function ( string $text ): string {
$text = mb_strtolower( $text, 'UTF-8' );
$text = strtr( $text, array( 'ου' => 'ou', 'ού' => 'ou' ) );
return strtr(
$text,
array(
'α' => 'a', 'ά' => 'a', 'β' => 'v', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'έ' => 'e',
'ζ' => 'z', 'η' => 'i', 'ή' => 'i', 'θ' => 'th', 'ι' => 'i', 'ί' => 'i', 'ϊ' => 'i',
'ΐ' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => 'x', 'ο' => 'o',
'ό' => 'o', 'π' => 'p', 'ρ' => 'r', 'σ' => 's', 'ς' => 's', 'τ' => 't', 'υ' => 'y',
'ύ' => 'y', 'ϋ' => 'y', 'ΰ' => 'y', 'φ' => 'f', 'χ' => 'ch', 'ψ' => 'ps', 'ω' => 'o',
'ώ' => 'o',
)
);
};
add_filter(
'sanitize_file_name',
static function ( $filename ) use ( $greeklish ) {
if ( ! preg_match( '/\p{Greek}/u', (string) $filename ) ) {
return $filename;
}
$filename = preg_replace( '/[^a-z0-9._-]+/', '-', $greeklish( (string) $filename ) );
return trim( preg_replace( '/-{2,}/', '-', $filename ), '-' );
},
5
);
Notes, where it runs and what it was tested with
Switching from the plugin
- Install the snippet in Snipfire (it arrives switched off).
- Deactivate AutoConvert Greeklish Permalinks.
- Switch the snippet on. Snipfire checks the code and test-loads your site first.
- Check the page it affects, then delete the plugin.