Skip to content

Conversation

@carosioa
Copy link

@carosioa carosioa commented Jun 26, 2025

Description

Currently, when editing a post in Gutenberg, the author terms are not saved to the post itself until after the editor has finished saving:

subscribe(() => {
if (isSavingPost()) {
checked = false;
} else if (!checked) {
const { id } = getCurrentPost();
const authors = getAuthors(id);
saveAuthors(id, authors);
checked = true;
}
});

This creates issues when we have PHP methods hooked into any of the actions related to post saving and publishing (like save_post, post_status_transition, etc), as those hooks have already ran once the authors are saved, so the PHP code does not have access to the correct / up to date author terms for the post.

See issues: #1024 & #1053

To reproduce this, add a hook on save_post with 9999 priority and then do wp_get_post_terms( $post_id, 'author'). If the post hadn't been saved before, that will return empty. If it has been saved, but the authors have been edited since save, it will return outdated info.

My suggested PR is to add the author terms in the post at the same time as the React data store is updated, here:

https://github.com/carosioa/Co-Authors-Plus/blob/37dccd11c057d92646c569b3fb6ece55f169594c/src/components/co-authors/index.jsx#L102-L118

Then you can totally remove the subscribe action that is making api calls to add the authors and the state of the post in WordPress will always match with the Coauthors box.

I did have to add each author term ID to the API response for the search box so it that was easily accessible in React. Not sure if there is a better place for looking that up than here:

https://github.com/carosioa/Co-Authors-Plus/blob/37dccd11c057d92646c569b3fb6ece55f169594c/php/class-coauthors-endpoint.php#L210-L224

Deploy Notes

No new dependencies.

Steps to Test

  1. Switch to branch and start a new post in Gutenberg
  2. Add multiple authors
  3. Check in Console and wp.data.select('core/editor').getEditedPostAttribute( 'coauthors' ) should be up to date / show the same users as wp.data.select('cap/authors').getAuthors()

@carosioa carosioa changed the title Fix/gutenberg save Fix Gutenberg saving authors after PHP save / update post hooks have already run Jun 26, 2025
@carosioa carosioa changed the title Fix Gutenberg saving authors after PHP save / update post hooks have already run Fix Gutenberg saving authors after PHP save post hooks have already run Jun 26, 2025
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.

1 participant