WordPress Revisions Restore Everything. Here Is How to Restore One Block

A whole paragraph was deleted last week, the post was edited 10 times since then, so when we went to the revisions and clicked “Restore This Revision” -> the whole paragraph came back to its place and the 10 edits after that went away. There is a way to copy one block back by hand. This can also be done inside the editor with the help of a plugin.

How are blocks stored?

The block editor saves all the blocks as normal HTML inside the post_content column. The blocks are wrapped inside comments like <!-- wp:paragraph --> ... <!-- /wp:paragraph --> -> the settings for the block are stored as JSON inside the opening comment <!-- wp:heading {"level":3} -->.

If a block contains other blocks inside it for e.g. group or columns -> the inner blocks would sit inside the parent block.

parse_blocks() -> this would read the blocks and return them in the block structure.

serialize_block() -> this would take the block structure and convert it into the HTML markup.

There is no permanent ID in the block markup, the block editor gives every block a fresh clientId (UUID v4) on the load of the editor -> that means the client ID would be different after every page reload.

How do revisions work?

WordPress creates a revision which is a copy of the current post, the type of it is revision, the revision post type points to the post which is being revised.

The following fields are kept intact for revisions:

  • post_title
  • post_content
  • post_excerpt

A new revision is created on every update of the post -> post_updated -> wp_save_post_revision().

WordPress by default has the constant WP_POST_REVISIONS which is true -> it means all the revisions are kept. If we set it to false or 0 -> it would mean no revisions would be kept. We can also send a number to it and it would mean that the number of revisions would be kept.

We can override this value through the filter wp_revisions_to_keep.

Why is restore all or nothing?

On the Revisions screen we get a slider bar which helps us compare any two revisions against each other.

Below the slider we get options like “Restore This Revision” and “Restore This Autosave”.

The function used behind the scenes for this is wp_restore_post_revision() which just copies the title, content and excerpt back to the current post.

The thing here is, the whole post_content is replaced with the content from the revision, every block from it is replaced. So if we had edits after that revision, they would be gone from the post, they would still be in newer revisions, they would have to be redone by hand.

Same thing happens when we select “Restore This Autosave”.

Why does undo not help?

The editor provides us the undo functionality to undo the last few actions we performed, but how does it know what is to be undone? When we use the block editor, the history of the current document is saved in the core-data store in our browser memory.

On every load of the editor, the history is cleared and left empty, and that’s why if we reload the page or close the tab our history is also gone. This is also the reason why the edits another person made on the post are not there in our undo history. Edits you have made yesterday at 7.20pm would not be there in your history if you check it at 11pm today.

The autosave does not save the intermediate drafts of the posts. The autosave feature only saves the latest unsaved state to the database.

The revisions would be the only copy that would survive.

We usually tend to use Ctrl+Z a lot to undo the actions -> it remembers exactly as long as the tab is open.

How do blocks get lost?

  • You’ve lost one block, but the rest of the post looks intact (can be because of a deleted block, it can be because of rewriting, maybe you’ve rewritten the post and then the old wording is required back at a later time).
  • When we are editing a post and another editor clicks “Take over” they can now edit the same post and save over our edits.
  • You’ve changed some settings of your block (colours, spacing, layout), now you want the old look back (can be the same case with the Site Editor templates and template parts).

After all these actions, only the old version of the post will be left in the revisions.

Restoring one block by hand

We can use the Revisions screen to find a version of the post which contains the block we want.

The compare view shows us the raw markup for both the previous and current version of the post. From here we can copy the block content by selecting all the content from the <!-- wp:... --> opening comment for the block and copying till the closing comment for the block.

To get it into our current post we can use the Code editor (Ctrl+Shift+Alt+M, ⇧⌥⌘M on Mac) and paste the code at the right spot.

If our block markup is not properly created, we might receive a message saying Block contains unexpected or invalid content. We must check the preview of the post before we update it.

Copying only the text from the compare screen will not get us the block with all the settings, it would only get us the text inside the block.

Picking the right revision

With the slider, we can see the history of the post one step at a time, we can also enable the checkbox “Compare any two revisions” to be able to compare any two revisions even if they are not adjacent to each other in the revisions list.

In each step of the slider it displays the author and the time of the action performed.

It labels all the autosaves as well.

The revisions slider supports the arrow keys, we can use the arrow keys to move through the revisions.

Restoring with Axle Block Restore

This is a free plugin which works inside the block editor.

In the editor after selecting a block we can see a block menu -> the three dots ⋮ options in the Block toolbar.

In that we can see “Restore from a revision…”, we can pick a revision from the list of revisions to restore that particular block to.

It shows us the versions of that particular block and picks the best possible match to restore, we can choose to “Replace current block” or “Insert below it” to compare them.

It matches on the basis of the block type, position and number of words in common with the current block.

It also shows us the “Show changes” button which would show us word-by-word difference between the current block and the block in the selected revision.

This works for all the blocks including the blocks from other plugins.

Each of these restore actions works as a normal edit action, if we press Ctrl+Z it would undo it.

Also, nothing is saved to the database until we hit the Update button.

Bringing back a deleted block

Under the editor main menu (the ⋮) -> we have an option called “Restore blocks from a revision”.

From this, we can see a list of blocks with some labels, “Not in the post now”, “Changed since” and “Same as now”. We can click “Restore it here” and this would restore that block to where it was at the time of the revision.

We have the “Removed blocks” tab, this would show us the blocks which are there at a previous revision, but are not there in the current post.

We can also see who saved the last revision that had it, when it was saved and we can also “Put it back where it was” -> if we click on this the blocks would be added to the position they had. We can also “Find text in old revisions” -> if we have added some words to the block, we can search for those words in the previous revisions and can see the blocks which had those words inside them.

Content, styling and groups

  • “Restore content only”: it will restore the words and links and everything inside the inner blocks, but the styling would remain the same as of the current edited post.
  • “Restore styling only”: this would restore the old look and feel of the block that means the styling, but the content inside the blocks would remain the same.

The content refers to the attributes of the block where the role: content is specified under the block.json file or the text. Rest of the things are considered as styling.

For elements like group or columns -> the whole thing would be replaced. That means if you undo the current change by a single Ctrl+Z, the whole thing would be undone.

Templates and other posts

In the Site Editor, when we create templates and template parts and we save them, then they have revisions registered against them.

If we check the source of the template, it would still be pointing to the theme files until we save it for the first time. After that, revisions would start accumulating.

When we add a block into a template part, it would not add revisions to the template post, instead it would add revisions to the template part.

There’s another tab in the editor, labelled as “Another post”, in this we can browse through the posts and select the one we want, then we can select a block from there and add it to our current post.

On every post it checks if the edit_post capability is assigned to us and only adds it in the post list if we are able to pass that check.

In WordPress we have the default command palette which can be accessed by pressing Ctrl+K / ⌘K, in the command palette it says “Show removed blocks”, it also holds three other things.

Limits

If WP_POST_REVISIONS is set to false -> this means that your site is not having revisions, there is nothing to restore back to.

Or your revisions are capped and only the last N versions are there.

Or the wrong block is matched to the block markup, we can pick up another version of the block from the list of available revisions.

Block Rewind is a different approach of the block management, not implemented in the WordPress core.

With this plugin the revisions of every block are stored in the browser.

With this the history of the block is started at the time of install and is saved about once a minute.

While in the WordPress default revisions system, every saved version of the post is considered, no matter who saved it or when it was saved, even the revisions created before the Axle Block Restore plugin was installed would work in this way.

You can try out the demo of Axle Block Restore on try.axlewp.com/axle-block-restore.