JavaScript for WordPress Forums Gutenberg Development Meta data – default value

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
  • #87315


    When you add a meta field as attribute, it does not take into account the default value specify in the attributes definition.


    const attributes = {
    	identifier: {
    		type: 'string',
    		default: 'home',
    		source: 'meta',
    		meta: 'blocks_identifier'
    export default attributes;

    The default value is ignored and the meta field is displayed empty.
    I found in gutenberg github a solution for that:

    				'type'         => 'string',
    				'single'       => true,
    				'show_in_rest' => [
    					'schema' => [
    						'type'    => 'string',
    						'default' => 'home',

    When adding the block on the page, the meta field has the default value BUT if you do not change it, it’s not saved in database.
    I checked the POST request when saving the post and I can see that the meta are included in it.

    Any idea?


    Zac Gordon

    Correct. I believe you do not want to have a default value for the meta field.

    One simple way here is if the value of the attribute is empty you can just write a conditional to use another value.


    Thx Zac, but looking in the POST request, I can see that the attribute has a value : the default one. So I do not understand why it’s not saved in database.

    Zac Gordon

    If you load a default value then save the post does that save the default value to meta?

    Sorry not fully understanding what you’re asking!


    Sorry to not be so clear 🙂
    When I load a default value and save the post, it does not save the default value to meta but the value is present in the POST request.

    POST request URL:

    POST request meta section:

    meta {
    blocks_identifier: "home"

    When checking the database, in postmeta, the meta has not been added.
    Hope this is more clear.


    Zac Gordon

    Okay, so I believe what you’ve determined is it doesn’t work.

    If this is expected behavior from GB then you would need to create a work around to have this actually work. I’d suggest pinging the WP Core Editor Slack Channel to ask if this is a bug or expected.

    As mentioned initially I didn’t think default would work with meta fields, but it may be that this is a bug..


    okidoki. I will keep you informed about their answer.

    have a nice day!

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.