AdvancedCustomFields / AdvancedCustomFields/acf

ACF post preview and revisions completely broken

Open
#903 22 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
945
Forks
197
PR merge metrics
No merged PRs in 30d

Description

Describe the bug
The internal handling of ACF for revisions and previews seems to be completely broken in numerous ways when using the default WordPress Gutenberg editor. Any field groups created and attached to a post, either do not show at all when attempting to preview, or if using a workaround to remove the filter, they show values from the last "published" post.

TLDR; Preview does not work correctly for ACF fields if using the default WordPress Gutenburg editor

1.) ACF does not handle revisions correctly, and is using the revision ID when you call get_field (see end of issue for details), ultimately causing nothing to show because metadata does not exist on the revision post ID.

2.) Even with revisions disabled, preview does not work correctly which probably stems from the same issue above, and does not show anything on preview either.

To Reproduce
Steps to reproduce the behavior:

  1. Create a blank WordPress install
  2. Create a new field group, leave the defaults to show when post type is Post
  3. Add any kind of field (text, repeater, etc)
  4. Create a new post
  5. Add [acf field="some_field_value"] shortcode to output on the page
  6. Set any random value in the ACF field created above
  7. Publish
  8. Edit post, change the ACF field value to something different
  9. Click Computer icon on top right and select Preview in new tab
  10. Preview will load, and either show nothing or show the old value

Expected behavior
The value updated when editing to show correctly on the preview

Basic setup and functional when published:
image
image
image

Now change the field value:
image

Click preview in new tab:
image

Nothing shows:
image

Version Information:

  • WordPress 6.4.3
  • PHP 7.4 & 8.1
  • ACF PRO Version 6.2.7
  • Any Browser

Additional context

As a WordPress expert and seasoned plugin developer, here's what I found.

The problem exists in acf_get_valid_post_id which calls the acf/validate_post_id filter, which then is filtered in acf_revisions::acf_validate_post_id who then calls acf_get_post_latest_revision which returns the revision ID.

The problems stems from the fact that ACF does not store meta on the revision, but for some reason, it's specifically changing Post ID's to the revision ID, ultimately causing the function call to get_field to call get_metadata using the revision post ID, but the only meta that will ever exist on it is something like this:

[ '_acf_changed' => [ 0 => "1" ] ]

My assumption is this code was meant for the pre-Gutenburg era, as I also found that when WordPress automatically calls the autosaves REST endpoint (every 60 seconds or when you click Preview in new tab), ACF does not have any handling in there to pass any values that have been changed, and since they are not one of the default fields post_title, post_content, excerpt, etc nothing gets saved, and as such, ACF is trying to pull the value from the revision (or autosave if revisions disabled) that will never have anything on it, as ACF has specifically set to only allow _acf_changed as the only valid meta for revisions/autosaves.

Basically it seems that preview only works with ACF if you specifically force the site to use the classic editor. I've tested this on a completely blank install and replicated it numerous times.

Numerous previous issues opened regarding this:
https://github.com/AdvancedCustomFields/acf/issues/411
https://github.com/AdvancedCustomFields/acf/issues/186
https://github.com/AdvancedCustomFields/acf/issues/184

Gutenburg related issue:
https://github.com/WordPress/gutenberg/issues/16006

Regarding Gutenburg issue ... it seems they have no plans to support passing meta fields, so this should be something added/handled via ACF as it's been YEARS and still no resolution. Being as though ACF is so widely used, it makes the most sense that this should be resolved in ACF now that Gutenburg is the default editor -- switching to classic editor is just not an option for some.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with acf_get_valid_post_id, acf_revisions::acf_validate_post_id, and acf_get_post_latest_revision, then trace how Gutenberg's autosaves REST endpoint handles ACF values. Reproduce the issue using the listed WordPress and ACF versions with a field group and the shortcode. Done means edited ACF values appear correctly in Gutenberg post previews and revisions without breaking published values.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, wordpress
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.