WordPress Transliteration Without Plugins

WordPress Transliteration Without Plugins

Category: Tips, WordPress
Last modified on January 11, 2025

Introduction

When developing a website with the potential for posts in Cyrillic, you may encounter the issue of Cyrillic URLs. While having Cyrillic characters in a URL does not affect functionality, it can lead to aesthetically unpleasant results when sharing these links, for instance, in a messaging app.

For example, instead of seeing a clean URL, you might get something like this:

https://yanmet.com/blog/%D0%B7%D0%BD%D0%B0%D1%91%D0%BC%D1%81%D1%82%D0%B2%D0%B0

Such links are not only unattractive but might also discourage users from clicking on them. Additionally, the concept of a “human-readable URL” suggests using links that provide a clear idea of the content available on the target page.

To address these issues, developers use transliteration. In WordPress, there are plugins available that can handle transliteration with a single click. However, for simple cases, such as transliterating blog post URLs, I prefer to use a custom function. This approach eliminates the risks associated with plugins – such as potential discontinuation plugin support, suboptimal performance, or unnecessary extra features.

When to Use Plugins

If you need to transliterate complex systems like WooCommerce or don’t have any programming skills, I recommend exploring one of the plugins available in the WordPress repository.

Custom Transliteration Code

To use this method, you need to know at least a little about writing code.

This code is triggered when a post is saving to the database. Note that it always bases the permalink on the post title. This means that even if you manually edit the permalink, the transliterated title will still be saved in the database.

Just place the code below in your theme’s functions.php file.

// Provides posts slugs transliteration. add_filter( 'wp_insert_post_data', function ( array $data ) : array { if ( in_array( $data[ 'post_type' ], [ 'post' ] ) ) { $slug = mb_strtolower( $data[ 'post_title' ], 'UTF-8' ); $slug = strtr( $slug, [ 'а' => 'a', 'к' => 'k', 'х' => 'h', 'б' => 'b', 'л' => 'l', 'ц' => 'c', 'в' => 'v', 'м' => 'm', 'ч' => 'ch', 'г' => 'g', 'н' => 'n', 'ш' => 'sh', 'д' => 'd', 'о' => 'o', 'щ' => 'shch', 'е' => 'e', 'п' => 'p', 'ъ' => '', 'ё' => 'yo', 'р' => 'r', 'ы' => 'y', 'ж' => 'zh', 'с' => 's', 'ь' => '', 'з' => 'z', 'т' => 't', 'э' => 'e', 'и' => 'i', 'у' => 'u', 'ю' => 'yu', 'й' => 'j', 'ф' => 'f', 'я' => 'ya', ]); $slug = sanitize_title( $slug ); $data[ 'post_name' ] = $slug; } return $data; });

The following example demonstrates a transliteration map for the Russian language. You can modify it to suit your needs by adding or changing symbols in the 'a' => 'b' format, where:

  • a is the character to be replaced;
  • b is the character that will replace a.

Adding Post Types

The code includes a check for the post type and is currently set only for posts. To apply transliteration to other (custom) post types, simply add their slugs to the array:

if ( in_array( $data[ 'post_type' ], [ 'post', 'custom-post-type' ] ) ) { ... }

Belarusian Map

For transliteration of the Belarusian language, use this map by replacing the one in the code above:

$slug = strtr( $slug, [ 'а' => 'a', 'к' => 'k', 'ф' => 'f', 'б' => 'b', 'л' => 'l', 'х' => 'h', 'в' => 'v', 'м' => 'm', 'ц' => 'c', 'г' => 'h', 'н' => 'n', 'ч' => 'ch', 'д' => 'd', 'о' => 'o', 'ш' => 'sh', 'е' => 'e', 'п' => 'p', 'ы' => 'y', 'ё' => 'yo', 'р' => 'r', 'ь' => '', 'ж' => 'zh', 'с' => 's', 'э' => 'e', 'з' => 'z', 'т' => 't', 'ю' => 'yu', 'і' => 'i', 'у' => 'u', 'я' => 'ya', 'й' => 'j', 'ў' => 'u', ]);

Useful Resources

A widely accepted transliteration standard in Cyrillic-using regions is ISO-9. You can learn more about it on the respective Wikipedia pages: