Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ACF post preview and revisions completely broken #903

Open
tripflex opened this issue Mar 25, 2024 · 10 comments
Open

ACF post preview and revisions completely broken #903

tripflex opened this issue Mar 25, 2024 · 10 comments

Comments

@tripflex
Copy link
Collaborator

tripflex commented Mar 25, 2024

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:
#411
#186
#184

Gutenburg related issue:
WordPress/gutenberg#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.

@lgladdy
Copy link
Member

lgladdy commented Mar 26, 2024

Hey there,

We're aware of this issue, and there are a few long standing issues in this repo for it.

We have a plan to resolve this issue, but it requires us to remove the legacy meta box and transition to a whole new system for updating ACF fields in the block editor, this is a long term project for us.

We'll keep you updated on our progress over the next several releases of ACF.

@tripflex
Copy link
Collaborator Author

Hey there,

We're aware of this issue, and there are a few long standing issues in this repo for it.

We have a plan to resolve this issue, but it requires us to remove the legacy meta box and transition to a whole new system for updating ACF fields in the block editor, this is a long term project for us.

We'll keep you updated on our progress over the next several releases of ACF.

What can I do to help? I'm happy to do some development work if you can point me in the right direction. Thanks!

@ConnorWedia
Copy link

I am also experiencing an issue similar to the above where if I add some text and let it autosave (without modifying any ACF fields), when I go to revisions and view the autosave, ALL ACF fields get removed.

Paragraph added for autosave test:
added_paragraph

Autosave Response result:
autosave_response

Revision of the autosave, showing the removed ACF fields:
removed_acf_fields

Is there ANY fix for this please as our content writers are constantly re-writing & re-adding ACF content whenever the post autosaves.

Thanks

@lgladdy
Copy link
Member

lgladdy commented Apr 8, 2024

@ConnorWedia Just to clarify, is this a recent change with an ACF or WordPress update, or has this always been the case for you in the block editor?

@ConnorWedia
Copy link

ConnorWedia commented Apr 8, 2024

@lgladdy The issue has only been happening for a couple of weeks now, and we have since updated Wordpress to 6.5 (thinking it was related), PHP to 8.3, and obviously latest version of ACF.

@lgladdy
Copy link
Member

lgladdy commented Apr 8, 2024

Thanks @ConnorWedia - I'll raise it with the team here today and see the updates we made to fix (some) revisions made it worse for autosaves.

I will also note, we're prioritising a more immediate fix to this issue now given how recent WordPress updates have made things much worse for revisions and autosaves with ACF metadata.

@phil-sola
Copy link

@lgladdy is there any movement on previews and revisions with ACF Fields?

It works for me always after the first time, but the first time I hit preview after changing some ACF flexible fields, it never displays. Super frustrating!

@ErikBrendel
Copy link

ErikBrendel commented Jul 8, 2024

@ConnorWedia did you find a solution or workaround to the problem of all ACF values being cleared on autosaves? We're experiencing the same problem...

@cooperfellows
Copy link

Adding our voice to this issue. Working with ACF Pro and experiencing these same issues. Starting to become a problem with our content editors. Any roadmap that can be shared around the "immediate fix" you mentioned @lgladdy?

@lgladdy
Copy link
Member

lgladdy commented Sep 24, 2024

@cooperfellows We're actively working on a potential long term fix for this at the moment and have a working internal branch, we'll have more to share on which release will contain this shortly once we're confident everything is working correctly.

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

No branches or pull requests

6 participants