You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Dynamic blocks: default values for attributes aren't converted into html comments #7342
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:
Make a custom block
Add a sourceless attribute with default value
attributes: {myNumber: {type: "number",default: 1,// this number won't appear in the html comments}}
Do not alter the attribute
Save the content
var_dump() the $attribues in 'render_callback
5b. or look into database, the html comment doesn't contain 'number'
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
Reacted by Will Skora, Giorgos Sarigiannidis, Shashikant Yadav, Serhiy Zakharchenko, Lohen Florent, René Eschke, Damon Cook, Damien Chantelouve, Rob Migchels, Trevor Robertson and 1 more
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.
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.
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.
Reacted by Will Skora, Ed, Wojtek Ruszczewski, Shashikant Yadav, Ross Morsali, JJ Jacobs, Alex Standiford, Ben Sibley, Lohen Florent, Nomi and 1 more
Reacted by Will Skora, Damien Chantelouve and Dan Devine
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.
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.
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.
Reacted by Amor Kumar, Tom Phillips, Damien Chantelouve and Peter
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.
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.
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.
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.
Reacted by Trevor Robertson, Will Skora and Will Skora
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
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.
Activity
katerlouis commentedon Jun 19, 2018
Slight push on this one.
The more attributes I have, the more this is bothering me–
Is this intended?
danielbachhuber commentedon Jun 19, 2018
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
katerlouis commentedon Jun 19, 2018
@danielbachhuber Is there a trick / workaround which forces gutenberg to write the value in the comment?
danielbachhuber commentedon Jun 19, 2018
@katerlouis Not to my knowledge, no.
gziolo commentedon Jul 10, 2018
@matias or @aduth, do you happen to know how this can be solved with the current state of Gutenberg?
aduth commentedon Jul 10, 2018
With moving block definitions to the server, I expect the preparation of generating attributes for
render_callbackshould be accounting for the default values of attributes defined inregister_block_type.That default values are not encoded in comments is the expected behavior.
gutenberg/blocks/api/serializer.js
Lines 154 to 157 in 7a878e0
See also #1905
aduth commentedon Jul 10, 2018
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
defaultvalues assigned to attributes inprepare_attributes_for_render.gutenberg/lib/class-wp-block-type.php
Lines 144 to 146 in 7a878e0
elliotcondon commentedon Nov 2, 2018
Although closed, I would like to add to this topic.
An attribute's
default valueshould 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_callbackfunction 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 valuevalue within the PHPregister_block_type()function. This would provide a way for therender_callbackto 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 valuesetting 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.
danielbachhuber commentedon Nov 2, 2018
@aduth @mtias Given #2751 won't land for 5.0, does it make sense to implement this behavior? ^
22 remaining items
Add default border to the frontend
alexstandiford commentedon Jul 1, 2021
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:
gutenberg/lib/edit-site-page.php
Lines 151 to 170 in 459ab7f
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
attributesobject that's passed intoregisterBlockTypeas a spread operator.naumanahmed19 commentedon Jan 23, 2023
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.
tcmulder commentedon Apr 6, 2023
I'm experiencing this also trying to set a default backgroundColor value. Say I set the default in the block.json file:
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.
Fix: Default values of attributes not saving as serialized block comment
Product Collection: Add stock status filter (#9580)
isuke01 commentedon Feb 5, 2024
@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.
MadtownLems commentedon Apr 30, 2024
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.
BrennanButler commentedon May 25, 2024
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.
MadtownLems commentedon Nov 27, 2024
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.
MadtownLems commentedon Jul 7, 2025
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
Product Collection: Add stock status filter (woocommerce/woocommerce-…
ellatrix commentedon Nov 3, 2026
This was reopened in 2018 because registering blocks on the server, which gives
render_callbackthe attribute defaults, wasn't ready for WordPress 5.0. That has landed since. Tested on trunk with Latest Posts, a dynamic block whosepostsToShowdefaults 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.
Partly automated with help from Claude.