wp_revisions_to_keep()
Defines how many of the latest revisions (changes) should be stored in the database for a specific post.
This is a wrapper for:
- The constant
WP_POST_REVISIONS
. - The hook wp_revisions_to_keep.
- The hook wp_(post_type)_revisions_to_keep.
The value of the constant is passed to the first hook, then to the second, and the resulting int (result) is returned. That is, the function relies on the value of the constant WP_POST_REVISIONS
and allows changing the result through hooks.
The constant WP_POST_REVISIONS
is set in the wp-config.php file.
WP_POST_REVISIONS
can be equal to:
-
true
(by default) - an infinite number of revisions are saved.Default value of the constant is defined in the file /wp-includes/default-constants.php.
-
false
or0
- revisions will be disabled. integer
- how many revisions to keep. If 5 is specified, then the last 5 changes of the post will be saved.
Revision support is enabled when registering the post type register_post_type(). Or separately through the function:
post_type_supports( 'post_type', 'revisions' )
If the post type has revision support disabled, the value of the constant WP_POST_REVISIONS
is reset. However, even if revisions are disabled for the post type, they can still be enabled through hooks, for example for a specific post. See example 2.
There is also a function wp_revisions_enabled() - checks if revisions are enabled for the post.
Hooks from the function
Returns
Int
. The number of revisions that need to be stored for the specified post.
integer
- how many revisions to keep in the DB.-1
- keep all revisions in the DB.0
- do not save revisions at all.
Usage
wp_revisions_to_keep( $post );
- $post(WP_Post) (required)
- Object of the post.
Examples
#1 Find out how many revisions the post may keep
On a clean WordPress installation, find out what the revision limit of the very first post is.
// get the post with ID=1 $post = get_post( 1 ); // Get the revision limit for this post $qty = wp_revisions_to_keep( $post ); // it will print -1, i.e. an infinite number of revisions can be stored print_r( $qty ); //> -1
#2 Enable revisions for a separate post
Suppose we have a post type article
which does not support revisions. But we need to enable revisions for one particular post, for example, with ID 54.
add_filter( 'wp_revisions_to_keep', function( $num, $post ){ if( 54 === $post->ID ){ return -1; } return $num; }, 10, 2 );
Changelog
Since 3.6.0 | Introduced. |