revision 是早就有了,auto-draft 是最近才发现的,个人非常不喜欢这2个功能,偏偏 WordPress 还没有在后台中增加显式的关闭功能,所以更显得烦人。
revision 是你每保存一次 post 的时候,都把修改前的内容存成一个 revision,这样你就不用担心以前的版本找不到了。问题是,写 blog 又不是写代码,用得着这把牛刀么?就是写代码,也有不想保存的版本,基本上扔到 scm 里面就不会再看了呀。
禁用 revision 的方法,对目前的 WordPress 3.0 有效,在 wp-config.php 中添加:
define('WP_POST_REVISIONS', false);
define('AUTOSAVE_INTERVAL', 60000);同时也禁止了自动保存,多手工保存吧,或者本地写好了再 post 。
auto-draft 是这样出现的,当你 new 一个 post 的时候,以前是第一次保存的时候生成 id,现在则是打开 new 页面的时候就生成了,体现在数据库中 wp_posts.post_status 为 auto-draft。这种没有内容先保存的方法一般是用来避免多人同时保存时的写入数据冲突,可一般的 blog 会频繁产生这种情况么?更糟糕的是,auto-draft 类型的post 无法在 Posts 管理中进行编辑,也就是说如果你打开了 new post 页面,输入了一些内容,然后没有保存或者发布就离开了这个页面,那么数据库中就多了一条 auto-draft “僵尸记录”,你再也找不到它了。
auto-draft 目前好像没有方法关闭,但可以从数据库中把他们更改为 draft,以后当草稿修改成新文章就是了:
mysql> SELECT DISTINCT post_status, count(1) FROM wp_posts GROUP BY post_status;
mysql> UPDATE wp_posts SET post_status='draft' WHERE post_status='auto-draft';最后,贡献一个 php 脚本,自动把 revision 和 auto-draft 都修改成草稿 draft,并且找出数据库中不连续的 post id,把他们也都存成草稿,这样可以保持 url 中 id 的连续性,似乎更加美观和整洁。未经严格测试,请参考使用:
<?php
/**
* Find and recover un-continous id of WordPress post.
*
* Will make empty draft using these id for edit them later.
* Also change revision or auto-draft to draft.
*
* @package fwolfweb
* @subpackage wordpress
* @copyright Copyright 2010, Fwolf
* @author Fwolf <fwolf.aide+fwolfweb.wordpress@gmail.com>
* @since 2010-07-05
*/
require_once(’wp-config.php’);
// Connect db
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$conn) {
die(’Could not connect: ’ . mysql_error());
}
mysql_select_db(DB_NAME, $conn);
if (defined(’DB_CHARSET’))
mysql_set_charset(DB_CHARSET);
// Table prefix
if (!isset($table_prefix))
$table_prefix = ’wp_’;
// Any revision or auto-draft in db ?
$sql = "UPDATE {$table_prefix}posts set post_status = ’draft’ WHERE post_status = ’revision’";
mysql_query($sql, $conn);
$i = mysql_affected_rows($conn);
if (0 < $i)
echo "Converted $i revision to draft.<br />\n";
$sql = "UPDATE {$table_prefix}posts set post_status = ’draft’ WHERE post_status = ’auto-draft’";
mysql_query($sql, $conn);
$i = mysql_affected_rows($conn);
if (0 < $i)
echo "Converted $i auto-draft to draft.<br />\n";
// Got all id of posts
$sql = "SELECT id FROM {$table_prefix}posts ORDER BY id ASC";
$rs = mysql_query($sql, $conn);
$i = mysql_affected_rows($conn);
if (0 < $i) {
// Have posts, continue traversal all rows.
$ar_missed = array();
$i_row = 1;
while ($row = mysql_fetch_assoc($rs)) {
if ($i_row < $row[’id’]) {
// Got missed id, add to array
for ($j = $i_row; $j < $row[’id’]; $j++)
$ar_missed[] = $j;
$i_row = $row[’id’];
}
$i_row ++;
}
// There are missed id ?
if (0 < count($ar_missed)) {
foreach ($ar_missed as $id) {
$sql = "INSERT INTO {$table_prefix}posts
(id, post_title, post_status, post_type) VALUES
($id, ’$id’, ’draft’, ’post’)";
mysql_query($sql, $conn);
}
echo ’Inserted id: ’ . implode(’, ’, $ar_missed)
. " as draft.<br />\n";
}
}
// Close db
mysql_close($conn);
?>