1

I have a block that save post ID as attribute. The block is working but the console show me an error reporting "Content generated by save function:" is different from "Content retrieved from post body:".

The problem is that the post ID returns null at first glance and then retrieve the correct ID.

This is my code:

const { getCurrentPostId } = wp.data.select("core/editor");

export default function save( { attributes, className, props } ) {
    const post_id = getCurrentPostId();
    return (
        <form class={ className }>
            <input name="_form_id" type="hidden" value={ post_id } />
            <div style="display: none;">
                <input type="text" name={ '_secret_h' + post_id } value="" />
            </div>
            <InnerBlocks.Content />
        </form>
    );
}

How can I be sure that the ID is correctly retrieved?

2
  • Do you need to do this in the save function? Could you retrieve the post_id in the Edit and save it as an attribute?
    – Welcher
    Dec 23, 2020 at 15:10
  • I've tried, however the problem is that in the edit function is not available immediately. I don't know exactly what is the lifecycle, however dumping in the console gives undefined and after some cycle it gives the correct post id. Dec 25, 2020 at 18:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.