每次升级WordPress,我都要重新修改内核文件,禁用其自动草稿Auto-Draft、自动保存Auto-Save和文章修订版本Post Revisions功能,使文章ID连续。虽然繁琐,可是一直保持下来了,也不想半途放弃。
目前最新版本为3.4.2,经我测试方法如下:
1、打开 wp-config.php 文件,在 “$table_prefix = ‘wp_’;” 前面添加如下代码(注意,一定是“$table_prefix = ‘wp_’;”这行的前面):
define(‘WP_POST_REVISIONS’, false);
define(‘AUTOSAVE_INTERVAL’, false);2、找到并打开 wp-admin\post-new.php 和 wp-admin\post.php 这两个文件,将其 “wp_enqueue_script(‘autosave’);” 注释或删除掉。
//wp_enqueue_script(‘autosave’);3、打开 wp-admin\includes\post.php 文件,找到 “if ( $create_in_db ) {” ,查找大约 419 至 424 行:
if ( $create_in_db ) {
$post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
$post = get_post( $post_id );
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 {
修改成
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 ); } 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 {
此方法的作用是如果数据库已经有不可见的自动草稿日志,那么点击添加新文章时,将不会再自动产生新的不可见自动草稿,而是直接调用最早的一篇不可见草稿作为当前文章所需的数据库记录来使用,同时为避免ID不连续,删除了“删除早于7天的自动草稿”的代码语句。方法经本人亲测,针对WordPress 3.4.2有效。
WordPress 3.5
3.5版本在修改到
2、找到并打开 wp-admin\post-new.php 和 wp-admin\post.php 这两个文件,将其 “wp_enqueue_script(‘autosave’);” 注释或删除掉。
//wp_enqueue_script(‘autosave’);
这一步会出现,编辑文章出现syntax error, unexpected,试着比较一下代码,发现有的不一样。多了一行
if ( ‘attachment’ !== $post_type );
WordPress 3.4.2 post.php代码
WordPress 3.5 post.php代码
$active_post_lock = wp_set_post_lock( $post->ID );
if ( ‘attachment’ !== $post_type );
//wp_enqueue_script(‘autosave’);在 if ( ‘attachment’ !== $post_type ) 这行末尾添加一个; 编辑文章就不会出现错误
$active_post_lock = wp_set_post_lock( $post->ID );
if ( ‘attachment’ !== $post_type );
//wp_enqueue_script(‘autosave’);
还有后台编辑文章一直刷新的问题也消失了,不知是不是之前的代码的问题。
经过测试,自动保存默认保留一条、其他的完全禁止了,用WP Cleaner查看