How to Use Revisions in WordPress: Step-by-Step Guide
In WordPress,
revisions automatically save versions of your posts or pages as you edit them. You can view, compare, and restore these revisions from the post editor screen to recover or track changes.Syntax
WordPress revisions are accessed through the post editor interface or programmatically via functions. The main parts are:
get_post_revisions($post_id): Retrieves all revisions for a post.wp_get_post_revisions($post_id): Returns an array of revision objects.wp_restore_post_revision($revision_id): Restores a post to a specific revision.- Revisions appear in the editor under the Revisions section for easy browsing.
php
<?php // Get all revisions for a post $revisions = wp_get_post_revisions($post_id); // Restore a post to a specific revision wp_restore_post_revision($revision_id); ?>
Example
This example shows how to list all revisions of a post and restore the latest revision programmatically.
php
<?php $post_id = 123; // Replace with your post ID // Get all revisions $revisions = wp_get_post_revisions($post_id); if (!empty($revisions)) { echo "Revisions for post ID $post_id:\n"; foreach ($revisions as $revision) { echo "Revision ID: " . $revision->ID . ", Date: " . $revision->post_date . "\n"; } // Restore the latest revision $latest_revision = reset($revisions); wp_restore_post_revision($latest_revision->ID); echo "\nPost restored to revision ID " . $latest_revision->ID . "."; } else { echo "No revisions found for post ID $post_id."; } ?>
Output
Revisions for post ID 123:
Revision ID: 456, Date: 2024-06-01 12:00:00
Revision ID: 457, Date: 2024-06-02 15:30:00
Post restored to revision ID 456.
Common Pitfalls
- Not enabling revisions: Some sites disable revisions in
wp-config.php, so no versions are saved. - Confusing autosaves with revisions: Autosaves are temporary and separate from revisions.
- Restoring revisions deletes current content: Always back up before restoring.
- Large revision history can bloat the database; consider limiting revisions.
php
<?php // Wrong: Disabling revisions disables version history // define('WP_POST_REVISIONS', false); // Avoid this if you want revisions // Right: Limit revisions to 5 to save space define('WP_POST_REVISIONS', 5); ?>
Quick Reference
| Function/Feature | Description |
|---|---|
| wp_get_post_revisions($post_id) | Returns all revisions for a post as objects |
| wp_restore_post_revision($revision_id) | Restores a post to a specific revision |
| Revisions UI | Access revisions from the post editor sidebar |
| WP_POST_REVISIONS constant | Controls how many revisions WordPress saves |
| Autosaves | Temporary saves separate from revisions |
Key Takeaways
WordPress revisions save versions of posts automatically as you edit.
Use the post editor or functions like wp_get_post_revisions() to view revisions.
Restore previous versions safely but back up current content first.
Limit revisions in wp-config.php to prevent database bloat.
Autosaves are different from revisions and should not be confused.