Skip to content

Dynamic blocks: default values for attributes aren't converted into html comments #7342

Closed

Description

@katerlouis

Describe the bug
When an attribute isn't changed, the value will not be written into the serialized html comment, which in fact, makes it unaccessible for PHPs render_callback-function.

To Reproduce
Steps to reproduce the behavior:

  1. Make a custom block
  2. Add a sourceless attribute with default value
attributes: {
    myNumber: {
        type: "number",
        default: 1, // this number won't appear in the html comments
    }
}
  1. Do not alter the attribute
  2. Save the content
  3. var_dump() the $attribues in 'render_callback
    5b. or look into database, the html comment doesn't contain 'number'
  4. Be sad 'number' isn't there :'(

Expected behavior
To avoid syncing the defaults in both php and js, I expect gutenberg to save defaults into the html comment aswell.

Desktop (please complete the following information):

  • OS: MacOS High Sierra
  • Browser Doesn't matter
  • Version Doesn't matter

Additional context

  • Gutenberg Version 3.0.1

Activity

  1. katerlouis commented on Jun 19, 2018

    @katerlouis
    Author

    Slight push on this one.
    The more attributes I have, the more this is bothering me–

    Is this intended?

  2. danielbachhuber commented on Jun 19, 2018

    @danielbachhuber
    Member

    Hi @katerlouis,

    What you've documented is the current behavior, yes. I've flagged this for review; we may consider changing the behavior in the future.

    Related #4494 #5739 #2751

  3. katerlouis commented on Jun 19, 2018

    @katerlouis
    Author

    @danielbachhuber Is there a trick / workaround which forces gutenberg to write the value in the comment?

  4. danielbachhuber commented on Jun 19, 2018

    @danielbachhuber
    Member

    @katerlouis Not to my knowledge, no.

  5. gziolo commented on Jul 10, 2018

    @gziolo
    Member

    @matias or @aduth, do you happen to know how this can be solved with the current state of Gutenberg?

  6. aduth commented on Jul 10, 2018

    @aduth
    Member

    With moving block definitions to the server, I expect the preparation of generating attributes for render_callback should be accounting for the default values of attributes defined in register_block_type.

    That default values are not encoded in comments is the expected behavior.

    // Ignore default value.
    if ( 'default' in attributeSchema && attributeSchema.default === value ) {
    return result;
    }

    See also #1905

  7. aduth commented on Jul 10, 2018

    @aduth
    Member

    I'm inclined to close this under the umbrella of #2751, as it essentially becomes a non-issue once implemented.

    For near-term, as a plugin developer you're best off to register your block attributes on the server. They should be inherited into the client automatically. If there's an issue with this, it can be addressed separately.

    Otherwise, it's expected that the server is respecting default values assigned to attributes in prepare_attributes_for_render.

    if ( is_null( $value ) && isset( $schema['default'] ) ) {
    $value = $schema['default'];
    }

  8. elliotcondon commented on Nov 2, 2018

    @elliotcondon

    Although closed, I would like to add to this topic.

    An attribute's default value should not be ignored when generating the block HTML comment. Think of the "align" attribute which is used to set the width of a block (full, wide, default, etc).

    The Block API Handbook shows that this attribute can be given a default value, which is a good feature. For example, a custom "gallery" block type can default to "full".

    I'm not sure if this issue affects a standard block type (because the align attribute will be rendered as actual HTML), but the issue becomes apparent when working with a dynamic block type.

    A dynamic block relies on all the attributes to be available to its render_callback function as there is no HTML saved by the block. The "full" align attribute mentioned earlier is never sent to the callback which makes it impossible to know what the alignment should be.

    Upon further reading, it seems that the "solution" is to define the default value value within the PHP register_block_type() function. This would provide a way for the render_callback to find the same default value.

    The issue with this "solution" is that all dynamic blocks are vulnerable to having their "align" setting bulk updated if the developer decides to change this default value setting later on. The blocks don't have any "align" attributes saved within the HTML comments, and will default to the "new default value".

    This could cause a lot of headaches to web developers.

  9. danielbachhuber commented on Nov 2, 2018

    @danielbachhuber
    Member

    @aduth @mtias Given #2751 won't land for 5.0, does it make sense to implement this behavior? ^

  10. 22 remaining items

  11. added a commit that references this issue on Nov 21, 2020
  12. alexstandiford commented on Jul 1, 2021

    @alexstandiford

    I stumbled on this problem today. I'm inclined to pass the registered attributes from PHP to JS using a preloaded REST API endpoint.

    I'm suggesting something like what's done here:

    // Preload block editor paths.
    // most of these are copied from edit-forms-blocks.php.
    $preload_paths = array(
    '/?context=edit',
    '/wp/v2/types?context=edit',
    '/wp/v2/taxonomies?context=edit',
    '/wp/v2/pages?context=edit',
    '/wp/v2/themes?status=active',
    array( '/wp/v2/media', 'OPTIONS' ),
    );
    $preload_data = array_reduce(
    $preload_paths,
    'rest_preload_api_request',
    array()
    );
    wp_add_inline_script(
    'wp-api-fetch',
    sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload_data ) ),
    'after'
    );

    And add a REST API endpoint that retrieves the list of registered block attributes, as well. That could be merged in with the JavaScript's attributes object that's passed into registerBlockType as a spread operator.

  13. naumanahmed19 commented on Jan 23, 2023

    @naumanahmed19

    For a custom plugin, I am facing this issue, I am parsing blocks and using them as API, in that case, all default attributes are missing and there is no way to get values until the user touched or changes the value of the input

    for example:
    When you add an image using core/image block, it displays image dimensions(height/width) in block settings but not in markup. Then it's confusing as well that there are values but not getting in HTML.

  14. tcmulder commented on Apr 6, 2023

    @tcmulder

    I'm experiencing this also trying to set a default backgroundColor value. Say I set the default in the block.json file:

    {
    ...
    "attributes": {
    		"backgroundColor": {
    			"type": "string",
    			"default": "xyz"
    		}
    	}
    }
    

    In the editor, the appropriate class for the background color (i.e. "has-xyz-background-clor") gets applied, but it doesn't also get applied on the front end via get_block_wrapper_attributes.

    What's more, if I do manually choose a different background color than the default and publish, the editor and get_block_wrapper_attributes both apply it properly, but if I then manually re-select the background color I have set as the default in block.json, it no longer gets added via get_block_wrapper_attributes.

    As a result, not only am I unable to set a default background since it won't appear initially on the front-end, but also if I set a default background then that particular color is unable to be selected for the front end, manually or otherwise, though all other colors are selectable.

  15. isuke01 commented on Feb 5, 2024

    @isuke01

    @tcmulder Here is another plot in the issue.

    If you set default attribute like you did.
    Then If you try to disable Background colour - reset to no background colour for the block, it will seems to be correct in editor but this is not saved, if you try too refresh editor it will be back to default - which again, will not be applied on frontend :|

    This apply to all kind of attributes not only BGColour.

  16. MadtownLems commented on Apr 30, 2024

    @MadtownLems

    I'm quite surprised that the current state is the default behavior. I'm definitely in support of immediately inserting the appropriate default value attributes in the HTML comment markup for freshly inserted blocks. The current state, where the UI and editor will show colors and the like that aren't yet present in the post_content feels wrong.

  17. BrennanButler commented on May 25, 2024

    @BrennanButler

    This is an incredibly annoying bug that I am also experiencing. I have also found that once a default value has been added to an attribute, removing it does not revert the behaviour to what you would expect - the attribute is still not found within the block attributes/markup.

  18. MadtownLems commented on Nov 27, 2024

    @MadtownLems

    For a totally different block than before, I was trying to set default values for the block upon insertion, and am once again disheartened by the fact that this doesn't seem possible.

    I strongly believe that the following should be possible:

    Developers can set default attributes for blocks that are used when inserting the block. These are immediately present in the meta, and the UI immediately shows their values. Doing so should make no changes to previously inserted blocks.

    For example, we recently decided that - in our theme - buttons should default to a background color of red (one of our brand's colors). We'd like any NEW buttons, inserted from here forward, to be inserted with red backgrounds (and the UI shows red selected). Of course, users can then change the colors to whatever they want. And we also want to make no changes to previously inserted blocks - only new ones going forward. I believe this kind of default should be possible, but it does not appear to be the case.

  19. MadtownLems commented on Jul 7, 2025

    @MadtownLems

    Given that this issue seems to have landed at: "That is expected behavior for Defaults" (which I agree shouldn't be changed NOW), I've opened a new issue presenting this as a feature request instead: #70633

  20. ellatrix commented on Nov 3, 2026

    @ellatrix
    Member

    This was reopened in 2018 because registering blocks on the server, which gives render_callback the attribute defaults, wasn't ready for WordPress 5.0. That has landed since. Tested on trunk with Latest Posts, a dynamic block whose postsToShow defaults to 5: the saved markup is <!-- wp:latest-posts /--> with no value, and the front end shows 5 of 8 posts, so the server applies the default.

    Leaving defaults out of the saved markup is intended. Writing them in when a block is first inserted is now its own feature request, #70633, opened by @MadtownLems after the last discussion here, so I'm closing this in favor of that one.

    Saved markup without the default
    Front end shows the default 5 posts

    Partly automated with help from Claude.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] Block APIAPI that allows to express the block paradigm.[Feature] ExtensibilityThe ability to extend blocks or the editing experience[Type] EnhancementA suggestion for improvement.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions