[070407] MySQL Charset Hack for WordPress 2.1.3 ……
作者:屈超(Chappell.Wat) 发布时间:April 7, 2007 分类:程式::五代
[更新日志]
2007-4-7: 更新到 2.1.3
最近看了最新的 WordPress 2.0.4 ……
还是不支持 MySQL 4.1+ 的字符集特性……
当然……
这也不怪那群老外……
毕竟人家很少用到……
不过对于中文用户就显得很有必要了……UTF8 当然是首选方案……
我也针对 UTF8 编码下的 PHP 编程写过一篇日志……
而这次对 WordPress 2.0.4 的修改也正是处于这个目的:
为了以正确的 UTF8 编码储存中文字符……
(当然修改过后…… 您也可以使用其它编码…… 这里仅以 UTF8 为例……)不愿手动修改的朋友可以直接下载我做的 Hack 包……
直接覆盖原版文件即可……
(共修改 4 个文件……)
下载:
插件包:http://www.live-share.com/files/196216/Charset_Hack_for_WP2.1.3.rar.html
目录:
手动修改步骤如下:
1. 为配置文件添加数据库字符集项目:
WordPress 提供有一份 Sample 配置文件……
我们先为其增加字符集项目……
编辑 wp-config-sample.php 文件,
搜索:
define('DB_HOST', 'localhost'); // 99% chance you won't need to change this value
在下方添加:
// ** Charset Selection Hacked By Chappell.Wat (http://QuChao.com) ** //
define('DB_CHAR', 'utf8'); // When using MySQL 4.1+, you'd better set charset for your db
安装 WordPress 时……
程序会由安装向导生成新的配置文件……
为此我们得添加字符集项目的生成过程……
编辑 wp-admin/setup-config.php 文件,
搜索:
<li>Table prefix (if you want to run more than one WordPress in a single database)</li>
在下方添加:
<!-- Charset Selection Hacked By Chappell.Wat -->
<li>Database charset(When using MySQL 4.1+)</li>
继续搜索:
Table Prefix
<input name="prefix" id="prefix" value="wp_" size="25" type="text" /> If you want to run multiple WordPress installations in a single database, change this.
在下方添加:
<!-- Charset Selection Hacked By Chappell.Wat -->Database Charset
<input name="charset" id="charset" value="utf8" size="25" type="text" /> When using MySQL 4.1+ and you're using a charset other than latin1, you can set the charset here.
再搜索:
$dbhost = trim($_POST['dbhost']);
在下方添加:
// ** Charset Selection Hacked By Chappell.Wat ** //
$dbcharset = trim($_POST['charset']);
接着搜索:
define('DB_HOST', $dbhost);
在下方添加:
// ** Charset Selection Hacked By Chappell.Wat ** //
define('DB_CHAR', $dbcharset);
最后搜索:
case "define('DB_HOST'":
fwrite($handle, str_replace("localhost", $dbhost, $line));
break;
在下方添加:
// ** Charset Selection Hacked By Chappell.Wat ** //
case "define('DB_CHAR'":
fwrite($handle, str_replace("utf8", $dbcharset, $line));
break;
目录:
2. 为数据库类添加设置字符集的方法:
由于字符集是 MySQL 4.1+ 的特性……
因此我们需要先判断 MySQL 的版本……
然后还需要检查需要的 CodePage 是否被编译进 MySQL ……
处理如下:
编辑 wp-includes/wp-db.php 文件,
搜索:
function wpdb($dbuser, $dbpassword, $dbname, $dbhost) {
return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost);
}
function __construct($dbuser, $dbpassword, $dbname, $dbhost) {
替换为:
function wpdb($dbuser, $dbpassword, $dbname, $dbhost, $dbcharset) {
return $this->__construct($dbuser, $dbpassword, $dbname, $dbhost, $dbcharset);
}
function __construct($dbuser, $dbpassword, $dbname, $dbhost, $dbcharset) {
接下来添加设置字符集的方法:
同样是编辑 wp-includes/wp-db.php 文件,
搜索:
/**
* Escapes content for insertion into the database, for security
在上方添加:
/**
* Charset Selection Hacked By Chappell.Wat (http://QuChao.com)
* Detect the charset
*/
function charset($charset) {
// Check if version of Mysql is 4.1+
$version = mysql_get_server_info($this->dbh);
if($version >= '4.1') {
// Check that if the charset was compiled in
$existCharsets = mysql_query("SHOW CHARACTER SET like '" . $charset . "'", $this->dbh);
if(mysql_num_rows($existCharsets) >= 0) {
if(!mysql_query("SET NAMES '" . $charset . "'", $this->dbh)) {
$this->bail("
<h1>Error setting charset for the database!</h1>
\n"
."
Does your database is 4.1+?"
. "Have you compiled the charset into your mysql?
\n"
."
Refer to the <a href="http://www.quchao.com/">INSTALL</a> document "
."if in doubt
\n");
}
}
}
}
然后在选择数据库之后调用该方法:
仍然是编辑 wp-includes/wp-db.php 文件,
搜索:
$this->select($dbname);
在下方添加:
// ** Charset Selection Hacked By Chappell.Wat (http://QuChao.com) ** //
$this->charset($dbcharset);
目录:
3. 建表添加默认字符集参数:
由于服务器环境差异……
并不是所有安装 MySQL 4.1+ 的服务器都以 UTF8 为默认字符集的……
(比如很多米国服务器就是 latin1 的默认字符集……)
而 WordPress 不管你甚么语系就直接往数据库里头灌……
等你发现中文字符变成问号时就只有重新安装了吧……
因此我们必须在建表时添加默认字符集参数来加以控制……
编辑 wp-admin/upgrade-functions.php 文件,
搜索:
$allqueries = array_merge($cqueries, $iqueries);
在上方添加:
// ** Charset Selection Hacked By Chappell.Wat ** //
// Check if version of Mysql is 4.1+
$version = mysql_get_server_info($wpdb->dbh);
if($version >= '4.1') {
// Check that if the charset was compiled in
$existCharsets = mysql_query("SHOW CHARACTER SET like '" . $charset . "'", $wpdb->dbh);
if(mysql_num_rows($existCharsets) >= 0) {
$cqueries = str_replace("\n)", "\n) DEFAULT CHARACTER SET " . DB_CHAR . ';', $cqueries);
}
}
至此……
我们就已经为 WordPress 的数据添加了字符集选项……
这样不管你运行在甚么配置的 MySQL 下都不用担心数据乱码了……
目录:
Great idea, but will this work over the long run?
ms不错。飘过~
此修改同样适用于 2.1 Alpha 版本……
有兴趣的朋友可以自行修改……
呵呵……
看了那么多类似文章, 看来这篇是最好的, 收藏.
不错的样子。