Gutenberg Range Control default from meta value

The issue here is on the PHP side. First we need to figure out if the meta key has been saved before and send this information to the editor. Once in the editor we can assign a default value if the meta key is new.

The way to check this is using get_post_custom_keys. We need to check for a certain post id and a certain meta key. To send this information to JavaScript we can use a rest route, lets say sending a rest request with the post id and the meta key which returns a boolean; or localize the output for the current post and then check against the array.

Lastly in the component we simply need to check if it exists. If so we apply the default value value: banner_image_position_has_been_saved ? meta.banner_image_position : 50.

Let me know if something was not clear.

Leave a Comment