最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

urls - Can anybody provide me with a function to generate slugs?

programmeradmin0浏览0评论

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.

Thanks!

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.

Thanks!

Share Improve this question asked Dec 30, 2020 at 13:37 WilliamWilliam 536 bronze badges 4
  • 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/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 Commented Dec 30, 2020 at 14:03
  • yes, I need non-latin characters to be encoded as %xx – William Commented 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 Commented Dec 30, 2020 at 14:10
  • I don't believe a translation plugin will help here – Tom J Nowell Commented Dec 30, 2020 at 14:15
Add a comment  | 

1 Answer 1

Reset to default 2

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/wiki/わかもの#Japanese, you browser URL encodes the japanese characters to get the real URL: https://en.wiktionary/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 )

发布评论

评论列表(0)

  1. 暂无评论