Last update at :2024-05-10,Edit by888u
WordPress has a function to automatically save articles and record revisions. When editing an article, WordPress will automatically save the article you edit every 60 seconds by default, so you don’t have to worry about losing the edited article content whether your browser crashes or your computer shuts down or other circumstances. For the article revision record function, WordPress will save the record of each revision you make in the database. Of course, under normal circumstances, you can also see the revision record as shown below on the article editing page:
Due to the intervention of article revision and automatic saving, the ID of the article often becomes larger and larger. As of the time I published this article, the article ID has reached 1009, and I have actually published only 742 articles; Moreover, excessive article revision records do not seem to be a good thing for database optimization, so we finally decided to disable the article revision and automatic saving functions. Although it does not completely solve the problem, it will somewhat alleviate it. There are many ways to disable article revision and auto-save functions on the Internet, but they are all very similar. I checked some official WordPress documents and sorted them out based on the code I thought was good on the Internet. It is roughly as follows:
Method 1 Modify the WordPress configuration file wp-config.php
Find the wp-config.php file under the root directory of the site, and add the following code after "define('WP_DEBUG', false);":
/** Disable article revision function */ define('WP_POST_REVISIONS', false); /** Disable auto-save function */ define('AUTOSAVE_INTERVAL', false);
Through the above code, we directly disable the article modification and automatic saving functions. For some other friends, you may still want to keep the revision records of recent versions to prevent the article from being unrecoverable due to misoperation. In this case, we directly modify it to false. It can be a specific positive integer, such as the following:
/** Keep the latest 5 revision records */ define('WP_POST_REVISIONS', 5); /** Automatically save every 360s */ define('AUTOSAVE_INTERVAL', 360);
I use this method myself, the specific code is as follows:
//Automatically save every 10 hours define('AUTOSAVE_INTERVAL', 36000); //Disable revision define('WP_POST_REVISIONS',false);
Method 2 Modify the function file functions.php under your own theme
Generally, we can directly modify wp-config.php to disable the article revision and automatic saving functions. However, if we want to add a custom option to enable or disable article revision in some themes, we can try to modify the theme. functions.php file and configure the corresponding options in the theme options. On how to add this function to the theme’s functions.php file, you can refer to the following code:
/** Disable automatic saving */ add_action('wp_print_scripts', 'qgg_not_autosave'); function qgg_not_autosave() { wp_deregister_script('autosave'); } /** Disable article revision */ add_filter( 'wp_revisions_to_keep', 'qgg_wp_revisions_to_keep', 10, 2 ); function qgg_wp_revisions_to_keep( $num, $post ) { Return 0; }
Similarly, if you want to set the number of article revisions to retain, you can use the following code:
//Set the specified article type (my_custom_post) to retain the latest 5 revisions add_filter( 'wp_revisions_to_keep', 'qgg_wp_revisions_to_keep', 10, 2 ); function qgg_wp_revisions_to_keep( $num, $post ) { If ('my_custom_post' == $post->post_type) { $num = 5; } Return $num; }
For related files of this function, you can simply check the revision.php file in the wp-includes folder of the program and the autosave.js file in the wp-includes/js folder.
About automatic drafts
As a side note, in addition to automatic saving and revisions, WordPress also has a very annoying feature called automatic drafting. In fact, for a long time, I couldn't tell the difference between automatic drafting and automatic saving. To put it simply, automatic saving means that when you write an article, the system will automatically back up the article and write it to the database according to the time interval; while automatic saving A draft means that from the moment you click "Write Article", new data has been written into the database, regardless of whether you enter content later, or even if you exit the editor.
Regarding disabling the automatic draft function, there is currently no simple and effective function code available. If you want to disable it, you need to modify the WordPress program itself. This is a bit troublesome and will not be discussed in this article.
Rendering:
In addition to the above functions that may cause article IDs to be discontinuous, WordPress will also occupy IDs when uploading new files, adding navigation menus, adding pages, etc., causing article IDs to be discontinuous. There is no good way to solve this kind of occupation.
In short, the structural design of WordPress has caused the discontinuous article ID problem that everyone is extremely disgusted with, but WordPress has no intention of modifying this unreasonable design. However, there is no default support for some common functions, and you can only rely on plug-ins or themes, which is really weird.
Recommended site searches: IP search, asp host space, anti-attack IP, foreign server, Guangdong website registration, check your own IP, Hong Kong vps host, virtual host server, US independent host, domain name registration,
发表评论