Return to Snippet

Revision: 42615
at March 8, 2011 06:36 by gryzzly


Updated Code
<?php
define('BASEPATH','');

// just a helper func to see how long it took to process slugs
function microtime_float(){
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();

// replace %root% and %secret% with your database credentials
$db = mysql_connect('localhost', 'root', 'secret');
// replace %wordpress_db% with your database
mysql_select_db('wordpress_db', $db);

$sql="";

// this dictionary is copied from the source of http://mywordpress.ru/plugins/rustolat/ 
// which is great, but only works for new posts
// in the situtation when I had > 500 posts it was easier to write this script
// than to go one by one updating slugs
$iso = array(
   "��"=>"YE","��"=>"I","��"=>"G","��"=>"i","�"=>"#","��"=>"ye","��"=>"g",
   "��"=>"A","��"=>"B","��"=>"V","��"=>"G","��"=>"D",
   "��"=>"E","��"=>"YO","��"=>"ZH",
   "��"=>"Z","��"=>"I","��"=>"J","��"=>"K","��"=>"L",
   "��"=>"M","��"=>"N","��"=>"O","��"=>"P","� "=>"R",
   "�¡"=>"S","�¢"=>"T","�£"=>"U","�¤"=>"F","�¥"=>"X",
   "�¦"=>"C","�§"=>"CH","�¨"=>"SH","�©"=>"SHH","�ª"=>"'",
   "�«"=>"Y","�¬"=>"","�­"=>"E","�®"=>"YU","�¯"=>"YA",
   "�°"=>"a","�±"=>"b","�²"=>"v","�³"=>"g","�´"=>"d",
   "�µ"=>"e","��"=>"yo","�¶"=>"zh",
   "�·"=>"z","�¸"=>"i","�¹"=>"j","�º"=>"k","�»"=>"l",
   "�¼"=>"m","�½"=>"n","�¾"=>"o","�¿"=>"p","��"=>"r",
   "��"=>"s","��"=>"t","��"=>"u","��"=>"f","��"=>"x",
   "��"=>"c","��"=>"ch","��"=>"sh","��"=>"shh","��"=>"",
   "��"=>"y","��"=>"","��"=>"e","��"=>"yu","��"=>"ya","�«"=>"","�»"=>"","â��"=>"-"
  );

// this is a name of file that will be generated to use later to actually update our DB
// it can be anything you want and by default it will be created in the same directory
// where this script is
$myFile = "slugs_fix.sql";
$fh = fopen($myFile, 'w') or die("can't open file");

# slugs
$q = mysql_query("select * from wp_posts where post_status = 'publish' and post_type = 'post'", $db);
while ($row=mysql_fetch_assoc($q)){

        $slug = $row["post_name"];
        $id = $row["ID"];

        // post_name is url-encoded � it's stored in a format
        // such as %D1%85%D0%B2%D0%BE%D1%81%D1%82
        $slug = urldecode($slug);
        // translate the string
        $slug = strtr($slug, $iso);
        
        $sql .= "update wp_posts set post_name = '" . $slug . "' where id = '" . $id . "'; \n";
        
        $stringData = $sql;
        fwrite($fh, $stringData);
        
        $sql="";
        $slug="";

}

mysql_close($old);

fclose($fh);

$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Transliterated slugs in $time seconds\n";

// okay, the file is written 
// and now it can be used like this:
// mysql -u root -p wordpress_db < slugs_fix.sql 
// after issuing this command your slugs should be updated

Revision: 42614
at March 8, 2011 06:35 by gryzzly


Updated Code
<?php
define('BASEPATH','');

// just a helper func to see how long it took to process slugs
function microtime_float(){
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();

// replace %root% and %secret% with your database credentials
$db = mysql_connect('localhost', 'root', 'secret');
// replace %wordpress_db% with your database
mysql_select_db('wordpress_db', $db);

$sql="";

// this dictionary is copied from the source of http://mywordpress.ru/plugins/rustolat/ 
// which is great, but only works for new posts
// in the situtation when I had > 500 posts it was easier to write this script
// than to go one by one updating slugs
$iso = array(
   "�"=>"YE","�"=>"I","�"=>"G","�"=>"i","�"=>"#","�"=>"ye","�"=>"g",
   "�"=>"A","�"=>"B","�"=>"V","�"=>"G","�"=>"D",
   "�"=>"E","�"=>"YO","�"=>"ZH",
   "�"=>"Z","�"=>"I","�"=>"J","�"=>"K","�"=>"L",
   "�"=>"M","�"=>"N","�"=>"O","�"=>"P","Р"=>"R",
   "С"=>"S","Т"=>"T","У"=>"U","Ф"=>"F","Х"=>"X",
   "Ц"=>"C","Ч"=>"CH","Ш"=>"SH","Щ"=>"SHH","Ъ"=>"'",
   "Ы"=>"Y","Ь"=>"","Э"=>"E","Ю"=>"YU","Я"=>"YA",
   "а"=>"a","б"=>"b","в"=>"v","г"=>"g","д"=>"d",
   "е"=>"e","�"=>"yo","ж"=>"zh",
   "з"=>"z","и"=>"i","й"=>"j","к"=>"k","л"=>"l",
   "м"=>"m","н"=>"n","о"=>"o","п"=>"p","�"=>"r",
   "�"=>"s","�"=>"t","�"=>"u","�"=>"f","�"=>"x",
   "�"=>"c","�"=>"ch","�"=>"sh","�"=>"shh","�"=>"",
   "�"=>"y","�"=>"","�"=>"e","�"=>"yu","�"=>"ya","«"=>"","»"=>"","�"=>"-"
  );

// this is a name of file that will be generated to use later to actually update our DB
// it can be anything you want and by default it will be created in the same directory
// where this script is
$myFile = "slugs_fix.sql";
$fh = fopen($myFile, 'w') or die("can't open file");

# slugs
$q = mysql_query("select * from wp_posts where post_status = 'publish' and post_type = 'post'", $db);
while ($row=mysql_fetch_assoc($q)){

        $slug = $row["post_name"];
        $id = $row["ID"];

        // post_name is url-encoded � it's stored in a format
        // such as %D1%85%D0%B2%D0%BE%D1%81%D1%82
        $slug = urldecode($slug);
        // translate the string
        $slug = strtr($slug, $iso);
        
        $sql .= "update wp_posts set post_name = '" . $slug . "' where id = '" . $id . "'; \n";
        
        $stringData = $sql;
        fwrite($fh, $stringData);
        
        $sql="";
        $slug="";

}

mysql_close($old);

fclose($fh);

$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Transliterated slugs in $time seconds\n";

// okay, the file is written 
// and now it can be used like this:
// mysql -u root -p wordpress_db < slugs_fix.sql 
// after issuing this command your slugs should be updated

Revision: 42613
at March 8, 2011 06:33 by gryzzly


Initial Code
<?php
define('BASEPATH','');

// just a helper func to see how long it took to process slugs
function microtime_float(){
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();

// replace %root% and %secret% with your database credentials
$db = mysql_connect('localhost', 'root', 'secret');
// replace %wordpress_db% with your database
mysql_select_db('wordpress_db', $db);

$sql="";

// this dictionary is copied from the source of http://mywordpress.ru/plugins/rustolat/ 
// which is great, but only works for new posts
// in the situtation when I had > 500 posts it was easier to write this script
// than to go one by one updating slugs
$iso = array(
   "Є"=>"YE","І"=>"I","Ѓ"=>"G","і"=>"i","№"=>"#","є"=>"ye","ѓ"=>"g",
   "А"=>"A","Б"=>"B","В"=>"V","Г"=>"G","Д"=>"D",
   "Е"=>"E","Ё"=>"YO","Ж"=>"ZH",
   "З"=>"Z","И"=>"I","Й"=>"J","К"=>"K","Л"=>"L",
   "М"=>"M","Н"=>"N","О"=>"O","П"=>"P","Р"=>"R",
   "С"=>"S","Т"=>"T","У"=>"U","Ф"=>"F","Х"=>"X",
   "Ц"=>"C","Ч"=>"CH","Ш"=>"SH","Щ"=>"SHH","Ъ"=>"'",
   "Ы"=>"Y","Ь"=>"","Э"=>"E","Ю"=>"YU","Я"=>"YA",
   "а"=>"a","б"=>"b","в"=>"v","г"=>"g","д"=>"d",
   "е"=>"e","ё"=>"yo","ж"=>"zh",
   "з"=>"z","и"=>"i","й"=>"j","к"=>"k","л"=>"l",
   "м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
   "с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"x",
   "ц"=>"c","ч"=>"ch","ш"=>"sh","щ"=>"shh","ъ"=>"",
   "ы"=>"y","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya","«"=>"","»"=>"","—"=>"-"
  );

// this is a name of file that will be generated to use later to actually update our DB
// it can be anything you want and by default it will be created in the same directory
// where this script is
$myFile = "slugs_fix.sql";
$fh = fopen($myFile, 'w') or die("can't open file");

# slugs
$q = mysql_query("select * from wp_posts where post_status = 'publish' and post_type = 'post'", $db);
while ($row=mysql_fetch_assoc($q)){

        $slug = $row["post_name"];
        $id = $row["ID"];

        // post_name is url-encoded – it's stored in a format
        // such as %D1%85%D0%B2%D0%BE%D1%81%D1%82
        $slug = urldecode($slug);
        // translate the string
        $slug = strtr($slug, $iso);
        
        $sql .= "update wp_posts set post_name = '" . $slug . "' where id = '" . $id . "'; \n";
        
        $stringData = $sql;
        fwrite($fh, $stringData);
        
        $sql="";
        $slug="";

}

mysql_close($old);

fclose($fh);

$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Transliterated slugs in $time seconds\n";

// okay, the file is written 
// and now it can be used like this:
// mysql -u root -p wordpress_db < slugs_fix.sql 
// after issuing this command your slugs should be updated

Initial URL

                                

Initial Description
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).

Initial Title
Transliterate existing cyrillic slugs (post_name) in wordpress

Initial Tags
wordpress

Initial Language
PHP