© / Posted in 知识如海 / June 22, 2009

WordPress从2.6版本开始,就加入了一个历史记录的功能,弄得在WordPress里发一篇文章时往往伴随着一大堆修订版。这只是对于多用户的Blog才有用,对于大多数个人用户来说,那些历史修订版是大量的垃圾数据,必然会影响到数据库的查询效率,浪费存储空间。

禁用 WordPress 文章修订功能的方法也有几种,网上流行的方法是:

在 wp-config.php 中添加以下代码:

 

define('WP_POST_REVISIONS', false);

关于 WP_POST_REVISIONS 这个变量的详细设置为:

true(默认)或者 -1:保存所有修订版本
false 或者 0:不保存任何版本(除了自动保存的版本)
大于 0 的整数 n:保存 n 个修订版本(+1 只保存自动保存版本),旧的版本将被删除。

另外,在 wp-settings.php 中,搜索 WP_POST_REVISIONS 可以看到这里的预定义,把:

$default_constants = array( 'WP_POST_REVISIONS' => true );

修改为:
$default_constants = array( 'WP_POST_REVISIONS' => false );

还是建议在 wp-config.php 增加代码来完成,毕竟升级博客后,wp-config.php 是不会被替换的。

参阅官方的说明:Revision Management

如果你觉得添加代码很麻烦的话,可以使用现成的插件来处理,比如:

super-switch - No Revisions - Revision Control

像 Delete-Revision 插件的作者和前阵子在网上看到一位博友的文章中有提到在 wp-config.php 中增加的代码并不能很好的终结文章修订功能,提出了另一种修改方法,既屏蔽了 WordPress 文章修订功能,同时保留了自动保存功能。摘录如下:

WordPress之所以能生成修订版,是因为在更新一篇文章之前,先执行了挂在“pre_post_update”钩子上的函数。在wp-includes/default-filters.php中,可以看到这一行:

add_action( 'pre_post_update', 'wp_save_post_revision' );

这样每次修改都会自动调用wp_save_post_revision这个函数来创建修订版记录。我需要做的就是把这一行注释掉,这样就不会在Update后生成修订版了。

但仅仅改了这一行还不行,经过我的测试,有一种情况仍然会产生修订版。那就是在修改已经发布的文章时,自动保存功能会产生新的修订版。为此,我通过分析自动保存的实现过程,确定了要修改的地方。在wp-admin/includes/post.php文件中,找到wp_create_post_autosave函数(大约在1030行附近)。通过阅读代码和注释,可以知道如果正在编辑的文章还没有自动保存过,就会创建一个修订版。为了让它仅仅起到保存的作用,需要将最后一行的

return _wp_put_post_revision( $_POST, true );

改成:
return edit_post();

整个函数如下:
function wp_create_post_autosave( $post_id ) {
	$translated = _wp_translate_postdata( true );
	if ( is_wp_error( $translated ) )
		return $translated;
 
	// Only store one autosave.  If there is already an autosave, overwrite it.
	if ( $old_autosave = wp_get_post_autosave( $post_id ) ) {
		$new_autosave = _wp_post_revision_fields( $_POST, true );
		$new_autosave['ID'] = $old_autosave->ID;
		return wp_update_post( $new_autosave );
	}
 
	// Otherwise create the new autosave as a special post revision
	// return _wp_put_post_revision( $_POST, true );
	return edit_post();
}

通过修改这两行代码,即保留了自动保存功能,又去掉了历史记录功能,另一个额外的好处是可以让文章ID连续增长。唯一不好的是需要修改源代码,这样在升级WordPress以后又要重新做一次。

本文仅有一篇评论 ↓↓


    1. welcome to our burberry outlet store ,there are many fashion and hight quality burberry bags wait for

      you!
      do you want to buy burberry bags for yourslf or your beat friends in sometimes?

      please come to our burberry outlet store.
      our burberry outlet store offer many new style burberry watches for you.do

      not miss the fashion burbery Burberry T-shirts.
      In burberry sunglasses stores it really is typically some sort of

      most effective style which precisely the top notch and several middle-class are typically in a region to afford
      Fouthy-six Putting on a new burberry watches for men and relish the

      summertime Does one prefers to invest in Burberry bags?


      as we all know the burberry brand is very famouse in the world.in burberry bags outlet store you

      will have a good time!
      there is new store,the name is burberry outlet !you are interested in new store,you will have a good

      time in there.
      If you use burberry bags ,you will feel very Summer feeling, so many girls put it

      down.
      evey women love the burberry shirts in life,the burberry can give you more

      confidence in sometimes.
      Jack bought the burberry sunglasses and the next day he came to the

      interview in the Burberry cheak shirt.


      Join forums that areparticularlymeant to talk about watches. If you are searching for burberry sale for women, then

      obviouslycheck out women's forum.
      One of the most popular British exports is the burberry outlet online .
      The particular burberry bags typically reveal the actual Burberry renowned examine layout

      which has been made popular by the garment range.
      The color of burberry ties are bright but with the characters of mature and steady
      the burberry shoes collection had occipied a large markey share and enjoys a massive

      fan following.
      I believe one's destiny of your burberry wallets long decrease jackets are

      often more large-scale progress.

    添加新评论 ↑↑