I’m trying to add extra toggle button to core gutenberg blocks and all are working fine except widget blocks and specially rss block.
- Added one button
- on button click add new attribute
- that attribute is not saving and block crashes
Here is the render API path:
domain.com/wp-json/wp/v2/block-renderer/core/rss?context=edit&attributes%5Bcolumns%5D=2&attributes%5BblockLayout%5D=list&attributes%5BfeedURL%5D=https%3A%2F%domain.com%2Fblog%2F&attributes%5BitemsToShow%5D=5&attributes%5BdisplayExcerpt%5D=false&attributes%5BdisplayAuthor%5D=false&attributes%5BdisplayDate%5D=false&attributes%5BexcerptLength%5D=55&attributes%5Bdatatext%5D=el1683204085047&post_id=2&_locale=user
It shows 400 Bad request and if you check that Rest API URL there is datatext attribute which is custom attribute I’m trying to add.
Error: react.js?ver=18.2.0:1120 Uncaught Error: Objects are not valid as a React child (found: object with keys {error, errorMsg}). If you meant to render a collection of children, use an array instead.
Please suggest me the best way to add extra attribute to core/rss wordpress block.
2
Answers
I replicated your error in WP 6.2 on the
core/rss
block. Extending the block with a new attribute via the filterblocks.registerBlockType
followed by editing then saving worked until afeedURL
was added.As the RSS block uses server side rendering, I reviewed the render_callback code as the ServerSideRender error read:
Error loading block: Invalid parameter(s): attributes
after addingfeedURL
, eg:No error
Error
As the RSS block is rendered using PHP, I would suggest a way around the issue to be creating a block variation and changing the render callback of your variation. Create your own PHP function that mirrors the default RSS render callback and also handles your extra parameter which should avoid the
Invalid parameter(s)
error.For completeness, below is the code I tested adding the extra attribute with:
JS
To add a custom attribute to a core Gutenberg block, you need to do the following:
it would look something like this:
Note the addition of the datatext attribute, which has a default value of an empty string.
Note the addition of the onButtonClick function, which sets the datatext attribute when the toggle button is clicked, and the use of the datatext attribute in the checked prop of the ToggleControl.
Note the addition of the data-datetext attribute, which includes the value of the datatext attribute.
Here are some documentation where you can find out more on blocks in Guttenberg:
Keep in mind that modifying core blocks can sometimes cause unexpected issues, so it’s important to thoroughly test your changes before deploying them on a live site. Additionally, be sure to keep backups of your code and database in case anything goes wrong.