在wordpress中音译现有的西里尔语slug(postu name)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在wordpress中音译现有的西里尔语slug(postu name)相关的知识,希望对你有一定的参考价值。

While working on the iteration of a blog with russian contents we had to change the slugs from cyrillic to transliterated russian.

[RusToLat](http://mywordpress.ru/plugins/rustolat/) is a great plugin that does just that, but unfortunately it only does the transliteration for new or "edited" posts (i.e. you have to open the post at least once and "edit" the permalink, then it will be transliterated).

Since this blog more than 500 posts this manual updating wasn't an option so we wrote this simple script. Maybe it will save somebody some time.

You'd probably better back-up your database before updating it (google for mysqldump syntax).
  1. <?php
  2. define('BASEPATH','');
  3.  
  4. // just a helper func to see how long it took to process slugs
  5. function microtime_float(){
  6. list($usec, $sec) = explode(" ", microtime());
  7. return ((float)$usec + (float)$sec);
  8. }
  9. $time_start = microtime_float();
  10.  
  11. // replace %root% and %secret% with your database credentials
  12. $db = mysql_connect('localhost', 'root', 'secret');
  13. // replace %wordpress_db% with your database
  14. mysql_select_db('wordpress_db', $db);
  15.  
  16. $sql="";
  17.  
  18. // this dictionary is copied from the source of http://mywordpress.ru/plugins/rustolat/
  19. // which is great, but only works for new posts
  20. // in the situtation when I had > 500 posts it was easier to write this script
  21. // than to go one by one updating slugs
  22. $iso = array(
  23. "��"=>"YE","��"=>"I","��"=>"G","��"=>"i","�"=>"#","��"=>"ye","��"=>"g",
  24. "��"=>"A","��"=>"B","��"=>"V","��"=>"G","��"=>"D",
  25. "��"=>"E","��"=>"YO","��"=>"ZH",
  26. "��"=>"Z","��"=>"I","��"=>"J","��"=>"K","��"=>"L",
  27. "��"=>"M","��"=>"N","��"=>"O","��"=>"P","� "=>"R",
  28. "�¡"=>"S","�¢"=>"T","�£"=>"U","�¤"=>"F","�¥"=>"X",
  29. "�¦"=>"C","�§"=>"CH","�¨"=>"SH","�©"=>"SHH","�ª"=>"'",
  30. "�«"=>"Y","�¬"=>"","�­"=>"E","�®"=>"YU","�¯"=>"YA",
  31. "�°"=>"a","�±"=>"b","�²"=>"v","�³"=>"g","�´"=>"d",
  32. "�µ"=>"e","��"=>"yo","�¶"=>"zh",
  33. "�·"=>"z","�¸"=>"i","�¹"=>"j","�º"=>"k","�»"=>"l",
  34. "�¼"=>"m","�½"=>"n","�¾"=>"o","�¿"=>"p","��"=>"r",
  35. "��"=>"s","��"=>"t","��"=>"u","��"=>"f","��"=>"x",
  36. "��"=>"c","��"=>"ch","��"=>"sh","��"=>"shh","��"=>"",
  37. "��"=>"y","��"=>"","��"=>"e","��"=>"yu","��"=>"ya","�«"=>"","�»"=>"","â��"=>"-"
  38. );
  39.  
  40. // this is a name of file that will be generated to use later to actually update our DB
  41. // it can be anything you want and by default it will be created in the same directory
  42. // where this script is
  43. $myFile = "slugs_fix.sql";
  44. $fh = fopen($myFile, 'w') or die("can't open file");
  45.  
  46. # slugs
  47. $q = mysql_query("select * from wp_posts where post_status = 'publish' and post_type = 'post'", $db);
  48. while ($row=mysql_fetch_assoc($q)){
  49.  
  50. $slug = $row["post_name"];
  51. $id = $row["ID"];
  52.  
  53. // post_name is url-encoded � it's stored in a format
  54. // such as %D1%85%D0%B2%D0%BE%D1%81%D1%82
  55. $slug = urldecode($slug);
  56. // translate the string
  57. $slug = strtr($slug, $iso);
  58.  
  59. $sql .= "update wp_posts set post_name = '" . $slug . "' where id = '" . $id . "'; ";
  60.  
  61. $stringData = $sql;
  62. fwrite($fh, $stringData);
  63.  
  64. $sql="";
  65. $slug="";
  66.  
  67. }
  68.  
  69.  
  70. fclose($fh);
  71.  
  72. $time_end = microtime_float();
  73. $time = $time_end - $time_start;
  74. echo "Transliterated slugs in $time seconds ";
  75.  
  76. // okay, the file is written
  77. // and now it can be used like this:
  78. // mysql -u root -p wordpress_db < slugs_fix.sql
  79. // after issuing this command your slugs should be updated

以上是关于在wordpress中音译现有的西里尔语slug(postu name)的主要内容,如果未能解决你的问题,请参考以下文章

Wordpress - 如何通过 slug 获取自定义帖子类型?

根据 Wordpress 中的自定义字段值批量重写帖子 slug

Unicode映射到语言

在Haskell中查找Char的Unicode脚本

wordpress:如何检查 slug 是不是包含特定单词?

WordPress:添加目录以发布具有功能的 slug