WordPress 3.4.2、3.5文章ID连续方法—禁用自动草稿、自动保存和文章修订

来源:互联网 发布:淘宝粉丝抢红包 编辑:程序博客网 时间:2024/05/22 00:23

每次升级WordPress都要重新修改内核文件,禁用其自动草稿Auto-Draft、自动保存Auto-Save和文章修订版本Post Revisions功能,使文章ID连续。

  • WordPress3.4.2,方法如下:

1、打开 wp-config.php 文件,在 “$table_prefix = ‘wp_’;” 前面添加如下代码(注意,一定是“$table_prefix = ‘wp_’;”这行的前面):

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

2、找到并打开 wp-admin\post-new.php 和 wp-admin\post.php 这两个文件,将其 “wp_enqueue_script(‘autosave’);” 注释或删除掉。

?
1
//wp_enqueue_script('autosave');

3、打开 wp-admin\includes\post.php 文件,找到 “if ( $create_in_db ) {” ,查找大约 419 至 424 行:

?
1
2
3
4
5
6
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 {

修改成

?
1
2
3
4
5
6
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,方法如下
    1、与上面WordPess3.4.2一样;

2、找到并打开 wp-admin\post-new.php 和 wp-admin\post.php 这两个文件,将其 “wp_enqueue_script(‘autosave’);” 注释或删除掉。

?
1
//wp_enqueue_script('autosave');

操作到这一步会出现:编辑文章出现“syntax error, unexpected”的问题。
试着比较一下代码,发现多了一行。

?
1
if ( 'attachment' !==$post_type );

 

WordPress_3.4.2_post

WordPress 3.4.2 post.php代码

WordPress_3.5_post

WordPress 3.5 post.php代码

3、在 if ( ‘attachment’ !== $post_type ) 这行末尾添加如下一行代码,编辑文章就不会出现错误,(还有后台“编辑文章一直自动刷新无法发表”的问题也消失了,不知是不是之前的代码所导致的问题。)

?
1
//wp_enqueue_script('autosave');

4、与上面WordPess3.4.2一样;

经过测试,自动保存默认保留一条自动保存文章,其他的修订版本什么的完全禁止了。用WP Cleaner查看如图:
WP_Cleaner

 

原创粉丝点击