Support

Account

Home Forums Gutenberg Updating fields within blocks from front end.

Solved

Updating fields within blocks from front end.

  • So I have blocks set up and working great. What needs to be done is on the front end, the client needs the ability to update a few fields within the block(s). I know for fields outside of Gutenberg, we can use update_field() or update_sub_field() and so on. Not sure how this is done now that we’re using Gutenberg.

    Initially I had my blocks as repeaters, so I was using the update_sub_field() function and that worked great. Upgrading to use the blocks, I took them out of the repeater since the blocks essentially act as one.

    My question is that is it possible and how would I go about updating fields within the Gutenberg blocks from the front end?

    Thanks in advance for any and all help.

  • An update on this if people would like to try to do the same thing. I do have a working function to get this done, it’s a bit hacky, but it’s working. It’s in a somewhat controlled environment, so this works for what we need.

    What’s being done is I’m taking the saved ACF block “JSON” from the wp_post table and spitting it out. Upon doing that, I’m doing a find/replace of all the extra characters that aren’t necessarily JSON friendly to make it JSON friendly. Once that’s complete, I’m using json_decode() to turn it into an editable format and update the fields that need updating. To save it, I use json_encode() to turn it back into JSON. Once again, while it may be unnecessary, I use json_decode($var, true) to turn it into a PHP array so I can add back the elements that are required with ACF.(<!– wp:acf/… and so fourth). Then I can save the post back to the database and successfully update my field(s) from the front end.

    Hopefully this helps anyone looking to do the same, or similar thing. If you’d like me to post my code, let me know…there is probably a better way of doing this.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Updating fields within blocks from front end.’ is closed to new replies.