Skip to content

Revisions: Save initial revision when inserting posts - #12272

Open
Sukhendu2002 wants to merge 8 commits into
WordPress:trunkfrom
Sukhendu2002:fix/30854-initial-post-revisions
Open

Sukhendu2002 wants to merge 8 commits into
WordPress:trunkfrom
Sukhendu2002:fix/30854-initial-post-revisions

Conversation

@Sukhendu2002

@Sukhendu2002 Sukhendu2002 commented Jun 23, 2026 •

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/30854

What is this PR about?

This PR makes wp_insert_post() save an initial revision when inserting a post whose post type supports revisions.

Current trunk only saves revisions when an existing post is updated. A post inserted directly in its final state therefore has no revision containing its original content.

Backwards compatibility

Existing function calls remain compatible because the new revision-author parameters are optional.

This intentionally changes revision counts for programmatically inserted revision-supported posts. Such posts now begin with one revision instead of zero.

Use of AI Tools

AI assistance: Yes
Tool(s): pi coding agent
Model(s): GPT-5.6
Used for: Investigating the revision flow, initial code skeleton, code review. Final implementation and tests were reviewed and edited by me.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions

Copy link
Copy Markdown

Hi there! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@apermo

apermo commented Jun 23, 2026

Copy link
Copy Markdown

Having worked with revisisions in late 2024, I am pretty sure that an initial revision is already created when a post is created.

Did you verify the reported bug is still valid, before creating the fix?

@Sukhendu2002

Copy link
Copy Markdown
Author

Yes, I re-verified against current origin/trunk. For the direct wp_insert_post() path covered by this ticket, trunk still creates 0 revisions on insert because wp_save_post_revision_on_insert() returns early when $update is false. I confirmed with a temporary PHPUnit test.

The behavior you remember may be from editor flows that create an auto-draft and then update it; that update does create a revision. This patch targets the bare insert path where a post is inserted in one call.

@apermo

apermo commented Jun 23, 2026

Copy link
Copy Markdown

That does make sense, thank you for pointing that out.

@Sukhendu2002
Sukhendu2002 marked this pull request as ready for review June 23, 2026 11:38
@github-actions

github-actions Bot commented Jun 23, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props sukhendu2002, adamsilverstein, apermo.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@adamsilverstein
adamsilverstein self-requested a review November 8, 2026 15:51
@adamsilverstein

Copy link
Copy Markdown
Member

Closing #1336 in favor of this PR.

@Sukhendu2002 I'll resolve the merge conflicts then give this a review. Thanks for your work here.

Resolve the set_up() conflict in the REST revisions tests: trunk dropped
the untyped `$revisions` property, so keep the initial-revision fixtures
from this branch without it. The global post tests added on trunk expect
the filter to see every revision, which now includes the initial one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018vwYjUwKmjrEva3fyXVCbp
@adamsilverstein

Copy link
Copy Markdown
Member

Claude suggested these two changes which I'll go ahead and push sicne they are small:

  1. The new author parameter can break existing hooks. The PR adds a second parameter, $revision_author, to the public wp_save_post_revision(). Any code that hooks that function with more than one argument, like add_action( 'save_post', 'wp_save_post_revision', 10, 2 ), would now pass a WP_Post object in as the author. PHP turns that into user ID 1 and raises a warning, so revisions get the wrong author. The review suggests leaving the public signature alone: when a post has no revisions yet and there's no logged-in user, use the post's author inside wp_save_post_revision().

do we need this new parameter? I'll remove for now, let me know if I am missing something.

  1. Wrong @SInCE version. It says 7.1.0, but trunk is 7.2-beta4, so it should be 7.3.0.

7.3.0 is right.

adamsilverstein and others added 2 commits November 8, 2026 11:01
The function is often hooked directly, and callbacks registered with
more than one accepted arg would pass a post object or ID in as the
new `$revision_author`, crediting revisions to the wrong user.

Pick the author inside wp_save_post_revision() instead: with no current
user and no existing revisions, the first revision is credited to the
post author. This also covers a first revision saved on update without
a current user, such as a cron edit of an imported post.

Also corrects `@since` to 7.3.0, since trunk is 7.2 beta.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018vwYjUwKmjrEva3fyXVCbp
The revision meta tests added on trunk read the oldest revision,
expecting the one saved by the first update. Posts now get a revision
on insert, so that is the second oldest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018vwYjUwKmjrEva3fyXVCbp

@adamsilverstein adamsilverstein left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up @Sukhendu2002, this is a much more complete approach than my old #1336, which I've closed in favor of this one. Moving the fix into wp_save_post_revision_on_insert() so it covers every status is the right call, and it's great to see the custom_css workaround finally go.

I pushed a few commits to get this moving again, hope that's ok.

Claude worked through the review and the follow-up commits with me, summary below:

Trunk merge (318ffe4). The branch conflicted in the REST revisions tests set_up(). Trunk dropped the untyped $revisions property, so the initial-revision fixtures were kept without it. The two global post tests added on trunk since (test_get_items_restores_global_post and test_get_items_without_global_post_leaves_it_unset) now expect the initial revision too.

wp_save_post_revision() signature (5746b52). The new $revision_author parameter on a public function that is often hooked directly was risky: a callback registered with more than one accepted arg, eg. add_action( 'save_post', 'wp_save_post_revision', 10, 2 ), would pass a WP_Post in as the author, and absint() turns that into user 1. The public signature is back to ( $post_id ), and the author is picked inside the function instead: with no current user and no existing revisions, the first revision goes to the post author. The private _wp_put_post_revision() keeps its new third param. One side effect worth noting: that also applies to a first revision saved on update without a current user (eg. a cron edit of an imported post), which used to get author 0.

@since is now 7.3.0, since trunk is 7.2-beta4.

New trunk tests (afc3413). The revision meta tests from #66329 read the oldest revision expecting the one saved by the first update, which is now the second oldest. The helper was renamed to get_first_update_revision_id().

The full PHPUnit suite passes locally with these changes.

Still open from the review, for discussion rather than blockers:

  • Opting out. The only way to skip the initial revision is removing wp_save_post_revision from post_updated, which also turns off revisions on update. On the ticket, @peterwilsoncc and @TimothyBJacobs floated a register_post_status() property; a simple filter in wp_save_post_revision_on_insert() might be enough. Worth adding?
  • Extra writes. Every programmatic insert of a type that supports revisions now writes a second row (importers, wp_global_styles, wp_template, wp_navigation, wp_block, custom_css). Seems like the right trade-off, but it belongs in the dev note along with the revision count change for REST and XML-RPC consumers.
  • Nit: in test_wp_save_post_revision_with_array_post_meta, the remove_action() / add_action() pair isn't wrapped in try/finally like the other tests here.

What do you think about the opt-out question?

Posts now get a revision on insert, so the fixture post reports one
more revision and the revisions collection includes the initial one.
Taken from the CI diff so the generated IDs match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018vwYjUwKmjrEva3fyXVCbp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants