Since Haibor has some concept of ID control and likes clean and simple things, I am very frustrated with the automatic draft saving function of the new version of WordPress. It causes a lot of waste of log IDs and makes the log IDs discontinuous. Therefore, get rid of this function! !
Let me explain the situation first: ID discontinuity is mainly caused by three functions. One is the automatic saving function Auto-Save, the second is the historical version Post Revisions, and the third is the automatic draft function Auto-Draft! Based on different wordpress versions, the solutions are different! Please select the corresponding solution!
Versions before wordpress 3.1 (excluding 3.1):
1. Open the wp-config.php file and add the following code in front of "$table_prefix = 'wp_';":
define('WP_POST_REVISIONS', false);
define('AUTOSAVE_INTERVAL', false);
2. Find and open the two files wp-adminpost-new.php and wp-adminpost.php, and comment or delete "wp_enqueue_script('autosave');".
//wp_enqueue_script('autosave');
3. Find and open the wp-adminincludespost.php file, find "if ($create_in_db) {" and add the following line before it:
$create_in_db = false;
WordPress 3.1 and later versions (including 3.1):
Since version 3.1, the program has made changes to the automatic draft function. The third step in the above method will cause a prompt that there is no operation permission when publishing articles in the background!
First of all, we should follow steps 1 and 2 in the above method to realize the disabling of the Auto-Save function and the historical version Post Revisions function. Next, let’s try to deal with the disabling of the Auto-Draft function!
Since the third step in the above method is invalid, you can no longer add the $create_in_db = false; code. At this time, the article publishing function is normal, but as long as you click "Add New Article", no matter whether you edit the article or not, as long as you do not click Save at the end. Or click Save as Draft (such as directly closing the editing page), the program will automatically add an invisible automatic draft log by default, with the title "Auto-Draft" and the status "auto-draft", and can only use phpmyadmin to extract data from the database. Delete! Therefore, if you don’t have a good blog idea, don’t click to add a new article easily!
Two fairly perfect solutions are given below. The basic code comes from a folder named "Pending" on the computer. The source cannot be found. Thank you to the party, the Internet, and the original author! Here I will organize and improve it and share it with everyone. At this point, non-ID control users can jump directly to the comments below haha! !
Note: The number of lines mentioned below is based on WordPress 3.2 version.
Option 1: Change the invisible auto-draft to a visible draft log draft.
Referring to step 3 above, find and open the wp-adminincludespost.php file, find "if ($create_in_db) {", and look for approximately 423 lines:
$post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
Change the above code to:
$post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'draft' ) );
Tip: This solution does not prevent the automatic creation of drafts when you click "Add New Article", but what is created at this time is a draft log that can be seen in the article list, and you can click to edit! Of course, if you click to add a new article multiple times, multiple draft logs will be generated, which is not very nice, haha!
Option 2: The principle is to obtain the earliest automatic draft and use it as the current article. This can maintain the continuity of article IDs based on the original automatic draft function, and remove the function of deleting automatic drafts 7 days ago.
Still find and open the wp-adminincludespost.php file, find "if ($create_in_db) {", and look for about lines 418 to 427:
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" );//Delete automatic drafts 7 days ago
foreach ( (array) $old_posts as $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 );
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 {
Replace with
if ( $create_in_db ) {
global $current_user;//Get the current login management 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" ); //Get the earliest automatic draft
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 {
The principle of option two has been explained above. Please read it with the code comments if you need it. The solution to option two is that if the database already has an invisible automatic draft log, then when you click to add a new article, new articles will not be automatically generated. Instead of invisible automatic drafts, the earliest invisible draft is directly called as the database record required for the current article. At the same time, in order to avoid ID discontinuity, the code statement "delete automatic drafts older than 7 days" is deleted. !
Okay, please take a seat if you need children's shoes. If you have better suggestions or questions, please leave a message to exchange!
This article is original to Nuodou. Please do not delete the link to this article when reprinting it: http://www.nuodou.com/item/622.html .
Thanks to haibor for your contribution