Add Snippet To Project
Worried about losing your progress while creating a post in WordPress due to unexpected browser crashes or internet connectivity issues? WordPress in-built autosave feature can be a real lifesaver in such scenarios. However, its default frequency might not always be aligned with your work pace. Hence, learning how to manipulate the autosave interval can save you some real-time headache. This article will guide you through the process of specifying the autosave interval in WordPress to ensure the maximum safety of your content.
function wpturbo_autosave_interval( $interval ) {
return 120; // in seconds
}
add_filter( 'autosave_interval', 'wpturbo_autosave_interval' );
In the given PHP code snippet, we are modifying the interval at which WordPress automatically saves drafts while editing. By default, WordPress autosaves the drafts every 60 seconds. This can be changed by using the ‘autosave_interval’ filter.
Here’s the step-by-step breakdown of how the code works:
At the beginning, we’re declaring a function named wpturbo_autosave_interval
which takes an argument $interval:
function wpturbo_autosave_interval( $interval ) {
Internally, the WordPress will call this function and pass the current autosave interval (in seconds) as the argument $interval.
Then we simply return 120; this suggests we’re setting the autosave interval to be 120 seconds:
return 120; // in seconds
}
That’s it for the function. It simply takes the current autosave interval and changes it to 120 seconds.
The second part of the code is using the add_filter
function which hooks a function onto a specific filter action, ‘autosave_interval’ in this case.
add_filter( 'autosave_interval', 'wpturbo_autosave_interval' );
So whenever WordPress decides to autosave a post, it will first apply this filter, in the process invoking our custom function wpturbo_autosave_interval
. This custom function modifies the WordPress provided autosave interval (which is by default 1 minute) to 2 minutes (or 120 seconds).
The end result is that WordPress will now automatically save drafts every 120 seconds instead of every 60 seconds. This can be particularly helpful if your work involves writing lengthy posts and you want to reduce potential losses from, say, a browser crash. Conversely, if you perform frequent edits and updates, you might prefer a shorter interval. Adjust the value returned by the function to best suit your needs.