The functions Wordpress uses to generate slugs are scattered in several functions and filters. I cannot find them all. Can anybody provide me a single php function to generate slugs? I do not need all the functionalities of Wordpress, just need to encode non-latin characters and remove illegal characters that are not allowed in url.

This post provides a function, but it cannot handle non-latin characters.


  • 1
    it's not possible to have non-latin characters, that's not how URLs work, e.g. an arabic URL only shows arabic characters because the browser is being useful, just like how & gets displayed as & when viewed. A slug with Arabic characters, or Japanese characters . e.g. en.wiktionary.org/wiki/…, if you visit that URL, those %E3 etc are displayed as etc in the address bar, but there is no character in the URL, and you can't have in a slug. If you modified the WP database to have a character then you'd get 404's
    – Tom J Nowell
    Dec 30, 2020 at 14:03
  • yes, I need non-latin characters to be encoded as %xx
    – William
    Dec 30, 2020 at 14:08
  • 1
    You can't use non-ASCII characters in URL unless percent-encoded. wp_unique_post_slug() is the only option. Try to find a plugin for your language, there are some, for example Chinese to Pinyin.
    – Max Yudin
    Dec 30, 2020 at 14:10
  • I don't believe a translation plugin will help here
    – Tom J Nowell
    Dec 30, 2020 at 14:15

1 Answer 1


This post provides a function, but it cannot handle non-latin characters.

That's because URLs can't have non-latin/ASCII characters.

Browsers might show non-latin characters to you, but it's just a user interface feature.

For example, if you visit this Wiktionary URL: https://en.wiktionary.org/wiki/わかもの#Japanese, you browser URL encodes the japanese characters to get the real URL: https://en.wiktionary.org/wiki/%E3%82%8F%E3%81%8B%E3%82%82%E3%81%AE#Japanese then URL decodes it when displaying the URL. The version with the percentage signs is the real URL.

Likewise with any other non-ASCII character codes, e.g. Arabic. If you create a post with the slug わかもの then save and check the database, you will not see わかもの, you will see %e3%82%8f%e3%81%8b%e3%82%82%e3%81%ae. Likewise the UI will show you the Japanese characters, but if you copy the link, you get the encoded version.

If we then update the database and change the slug of the post to わかもの, the post now can't be loaded, and we get a 404. That's because the slug is invalid and it's not possible to load a URL that matches that slug.

yes, I need non-latin characters to be encoded as %xx

You need to urlencode it echo urlencode( $slug )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.