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

[Feature Request] Save to post meta with block data #244

Open
hj-collab opened this issue Jun 12, 2022 · 9 comments
Open

[Feature Request] Save to post meta with block data #244

hj-collab opened this issue Jun 12, 2022 · 9 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@hj-collab
Copy link

I had raised a request regarding this before but I was not clear about it. I am okay if this feature request comes in the upcoming paid version. I had written my own custom block as I wanted to save data into post_meta as well as block attributres. I feel it will not be difficult to add. This is how I do it.

I write to attributes as well as meta on onChange. Check screenshot:-
image

Reason is that If I only read and save to meta then I won't enjoy auto save, post revisions and ability to use full editor area and not just small size sidebar panel. Check screenshot:-

unknown

@nk-o Please take a look and include this in your paid version.

@nk-o
Copy link
Collaborator

nk-o commented Jun 21, 2022

Hey.

I don't quite understand the reason. Currently data saved in the Meta automatically by Gutenberg, and you can output this attribute using function get_lzb_meta. It will be displayed both in editor and in frontend.

We don't need to duplicate data inside the block, you just work with Meta directly.

@hj-collab
Copy link
Author

I will make a video and explain.

@davidwebca
Copy link

Sorry to interject. @hj-collab do you mean that you want the attribute (such as SKU in your example) to save to the post_meta of the currently post being currently edited by that block?

@hj-collab
Copy link
Author

@nk-o

image

  1. You lose auto save benefit in the gutenberg editor when you tick "save in meta" as in the above screenshot.

Tick this option for any of the field you want, then go to editor for a post, type something in that field and refresh after 10 seconds. You will lose the changes. Saving to gutenberg block data keep the changes as autosave even if you close the tab, pc crashed, etc.

  1. Also undo does not work properly when this save in meta is ticked. It will undo char by char instead of word by word which happens when you save to block_data.

  2. You lose on post_revision benefit when this option is ticked.

How to solve the problem? You give an option to allow a block to be saved in both - block_deta and post_meta.

Reason: I want to save in post_meta in database but I also want the benefits of block_data (autosave, proper undo and post_revisions) I hope this is clear. Let me know if more info is required.

@davidwebca
Copy link

Oh I totally understand. It probably doesn't undo properly and doesn't save post_revisions properly because there's no markup changing in the block at all.

A simple solution would be to add a "revision" attribute that would simply be filled with the unix timestamp at the same rate as a normal block attributes save cycle. It would trigger post revisions, but there would still be problems with undo.

The other solution, as you said, would be to save to both block attributes and post meta, but prioritize post meta if it's found... though that would run the risk of creating conflicts with other blocks unless there's proper block-to-meta mapping - or left willingly naïve and leave the responsibility to the developer to manage this.

@hj-collab
Copy link
Author

@davidwebca Also autosave. Autosave is important when you are working with lots of field and lots of data to enter.

It should read from block data only else autosave, post revisions and undo won't work and write to block data and meta. I need meta because I have areas where getting meta data is easier then parsing block data. This also gives easy way to move out of block in case you face some limitations and can use the meta data with anything else.

@nk-o
Copy link
Collaborator

nk-o commented Jun 30, 2022

Understand, but I don't like the idea to save duplicates here (block attribute + meta field).

We are using the standard Meta block attribute and I see that it is deprecated now https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/#meta-source-deprecated. I will recheck this feature in the next plugin updates.

@nk-o nk-o added the enhancement New feature or request label Jun 30, 2022
@nk-o
Copy link
Collaborator

nk-o commented Jun 20, 2023

I have just changed the deprecated meta attributes and used useEntityProp directly, but it does not change anything, it works in the same way.

I have googled and looks like WordPress doesn't autosave meta at all, it is by design. If you have any ideas on how to improve the autosave function properly, I want to look at a solution.

Saving both block attribute and meta is a bad idea, because:

  1. Data duplication
  2. Autosaved post will save the block attribute, but not the meta, and we will have outdated meta in autosaved posts, which is not good too

@nk-o nk-o added help wanted Extra attention is needed question Further information is requested and removed enhancement New feature or request labels Jun 20, 2023
@hj-collab
Copy link
Author

@nk-o There is an open request at wp/gutenberg GitHub, and they have not been working on this issue for a long time. Since this requirement is limited to some users, we can wait until we have a proper solution, most likely from the core WP team.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants