WordPress Transliteration Without Plugins

WordPress Transliteration Without Plugins

Category: Guides, WordPress
Last modified on

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.

This function is ideal for automating slug transliteration based on post names in non-Latin alphabets.

  • Transliteration Logic: The code uses mb_strtolower to convert the term name into lowercase and strtr to replace Cyrillic characters with their Latin equivalents based on the provided map.
  • Slug Sanitization: The function sanitize_title ensures the resulting slug is URL-safe.

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' ] ) ) { ... }

How to Transliterate Terms

If you want to implement transliteration for WordPress terms, you can use the following code snippet. It processes the term name, converts it into a transliterated slug, and updates the term data accordingly. Here’s an explanation of key details:

// Provides terms slugs transliteration. function ym_translit_term ( int $term_id, int $tt_id, string $taxonomy ) : void { $term = get_term( $term_id, $taxonomy ); $slug = mb_strtolower( $term->name, '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 ); remove_action( 'saved_term', 'ym_translit_term' ); wp_update_term( $term_id, $taxonomy, [ 'slug' => $slug, ]); add_action( 'saved_term', 'ym_translit_term' ); } add_filter( 'saved_term', 'ym_translit_term', 10, 3 );
  • Preventing Infinite Loops: remove_action temporarily disables the saved_term hook to prevent recursive calls during wp_update_term. After the term update is completed, the action is re-enabled using add_action.

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:

Related Posts

by the same categories