禁用WordPress3.1.x自动保存与历史版本控制

2012-05-16  金城  1809

撤掉禁止WordPress自动保存与历史版本控制会造成ID的不连续,对于追求完美的人来说, 对自动保存和历史版本控制功能并不是太感冒, 呵呵。找了下, 汇总了一些关于禁止自动保存和历史版本控制的办法。

1. 首先在wp-config.php文件,在if ( !defined(‘ABSPATH’) )前面添加以下代码:

1
2
define('AUTOSAVE_INTERVAL',false);
define('WP_POST_REVISIONS',false);

2. 在wp-includes/default-filters.php中,加 // 注释掉wp_save_post_revision这个函数。

1
// add_action( 'pre_post_update','wp_save_post_revision' );

3. 在wp-admin/post.php 及 post-new.php文件中,继续注释掉

1
// wp_enqueue_script('autosave');

4.(1)找到wp-admin/includes/post.php文件中的 wp_create_post_autosave 函数,将该函数的最后一句:

1
return_wp_put_post_revision($_POST,true );

改为:

1
returnedit_post();

通过修改这两行代码,即保留了自动保存功能,又去掉了历史记录功能,另一个额外的好处是可以让文章ID连续增长。

(2)另外也可以从wp-admin/includes/post.php文件中找到这段(大概在399行处):

1
if($create_in_db){

在它下面找到这部分代码:

1
2
$post_id= wp_insert_post(array('post_title'=>__('Auto Draft'),'post_type'=>$post_type,'post_status'=>'auto-draft') );
$post= get_post($post_id);

修改为:

1
2
3
4
5
6
$post_auto_draft=$wpdb->get_row("SELECT * FROM $wpdb->posts WHERE post_type = '$post_type' AND post_status = 'auto-draft' LIMIT 1");
if($post_auto_draft){
   $post=$post_auto_draft;
}else{
   $post= get_post( wp_insert_post(array('post_title'=>__('Auto Draft'),'post_type'=>$post_type,'post_status'=>'auto-draft') ) );
}

(3)又或者可以将下面整个代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
if($create_in_db){
   // Cleanup old auto-drafts more than 7 days old
   $old_posts=$wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(),INTERVAL 7 DAY ) >post_date");
   foreach( (array)$old_postsas$delete)
       wp_delete_post($delete,true );// Force delete
   $post_id= wp_insert_post(array('post_title'=>__('Auto Draft'),'post_type'=>$post_type,'post_status'=>'auto-draft') );
   $post= get_post($post_id);
   // Below is added in 3.1
   if( current_theme_supports('post-formats') &&post_type_supports($post->post_type,'post-formats') &&get_option('default_post_format') )
       set_post_format($post,get_option('default_post_format') );
}else{
   $post->ID = 0;
   $post->post_author ='';
   $post->post_date ='';
   $post->post_date_gmt ='';
   $post->post_password ='';
   $post->post_type =$post_type;
   $post->post_status ='draft';
   $post->to_ping ='';
   $post->pinged ='';
   $post->comment_status = get_option('default_comment_status');
   $post->ping_status = get_option('default_ping_status');
   $post->post_pingback = get_option('default_pingback_flag');
   $post->post_category = get_option('default_category');
   $post->page_template ='default';
   $post->post_parent = 0;
   $post->menu_order = 0;
}

将上面的代码替换为:

1
2
3
4
5
6
7
8
9
10
11
12
if($create_in_db){
   global$current_user;
   $post=$wpdb->get_row("SELECT * FROM $wpdb->posts WHERE post_status = 'auto-draft' AND post_type = '$post_type' AND post_author = $current_user->ID ORDER BY ID ASC LIMIT 1");
   if( !$post){
       $post_id= wp_insert_post(array('post_title'=>__('Auto Draft'),'post_type'=>$post_type,'post_status'=>'auto-draft') );
       $post= get_post($post_id);
   }
   
   // Below is added in 3.1
   if( current_theme_supports('post-formats') &&post_type_supports($post->post_type,'post-formats') &&get_option('default_post_format') )
       set_post_format($post,get_option('default_post_format') );
}

OK,这些全部搞定了,自动保存功能还可以用,ID也能够连续了,perfect。

删除已经生产的版本控制命令为:

1
2
3
4
5
DELETEFROMwp_postmetaWHEREpost_idIN(SELECTidFROMwp_postsWHEREpost_type ='revision');
 
DELETEFROMwp_term_relationshipsWHEREobject_idIN(SELECTidFROMwp_postsWHEREpost_type='revision');
 
DELETEFROMwp_postsWHEREpost_type='revision');

将ID重新设置为连续,设置一个ID就要4条SQL语句,比如将ID为99的文章修改为ID为88,需要执行以下语句。

1
2
3
4
updatewp_postssetid = 88whereid = 99;
updatewp_term_relationshipssetobject_id = 88whereobject_id = 99;
updatewp_postmetasetpost_id = 88wherepost_id = 99;
updatewp_commentssetcomment_post_ID = 88wherecomment_post_ID = 99;

文章ID连续了,但继续发表新文章时,ID还是会从之前不连续时的最大数字往后编。
这时,修改下 auto_increment 的数值就OK了,即在 phpmyadmin 中执行下面语句:

1
altertablewp_posts AUTO_INCREMENT=n

n=梳理后的最大id+1,比如梳理后最后一篇文章的id是50,那么n就设为51。

另外如果觉得修改代码麻烦的,可以按照Super Switch 插件,将“自动保存”和“保存修订版本”设为禁止。

PS:补充另外一种更加简单有效的办法,在主题的 functions.php 文件里面加入如下代码即可

1
2
wp_deregister_script('autosave');
remove_action('pre_post_update','wp_save_post_revision');

或者

1
2
3
remove_action('pre_post_update','wp_save_post_revision');
wp_deregister_script('autosave');
remove_filter('the_content','wptexturize');

这样便可以避免更新升级wordpress版本的时候又去修改源代码的麻煩了。