New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extending WP Button with custom data attributes #16164
Comments
Same use-case (with the video-block), similar problem. My custom data-* attributes are apparently not even saved. I fear kses could mess-up with them and not being able to pinpoint it. |
I'm doing some cleaning and triage of issues and was wondering about the status of this one. Is this is still a problem? |
For me it is still happening. I'm just trying to add an |
Hey @enriquesanchez, thank you for asking :) I'd have to say, this is still an issue. As a workaround we have been adding users as admins because I don't believe this occurs if you're an admin. Not a great workaround though :/ |
I have found this nice tutorial, it might be useful for some. |
This really helped me. What I was missing was adding the attribute definition through the hook Step by step, this is what you should do if you want to add a new attribute to an existing block:
I might be missing something, I'm doing this on the fly. I'll probably extend on this later. 1) Define the attributeIn this examples, I will be adding a new attribute // Dependencies
import {
assign
} from 'lodash';
const { createHigherOrderComponent } = wp.compose;
const { InspectorControls } = wp.blockEditor;
const { ToggleControl } = wp.components;
wp.hooks.addFilter(
'blocks.registerBlockType',
'ta/image-block-photographer-attr',
settings => {
// If the block isn't a `core/image` block, we return the same settings
if(settings.name != 'core/image')
return settings;
// Else, we extend the attributes
settings.attributes = {
...settings.attributes,
showPhotographer: {
type: 'boolean',
default: '',
},
};
return settings;
}
); 2) Custom Controls - Extending the block's edit functionHere I extend the const withInspectorControls = createHigherOrderComponent( ( BlockEdit ) => {
return ( props ) => {
const { name } = props;
// If the block isn't a `core/image` block, we return an unmutated component
if(name != 'core/image')
return <BlockEdit {...props}/>;
// Else, we extend the component with controls to manipulate the new attribute.
return <TAExtendedImageBlock BlockEdit = {BlockEdit} {...props} />;
};
}, 'withInspectorControl' );
const TAExtendedImageBlock = (props) => {
const { BlockEdit, setAttributes, attributes } = props;
const { showPhotographer, id } = attributes;
return (
<>
<BlockEdit { ...props } />
<InspectorControls>
<ToggleControl
label="Mostrar datos del fotógrafo"
checked={ showPhotographer }
onChange={ () => setAttributes({ showPhotographer: !showPhotographer }) }
/>
</InspectorControls>
</>
)
}
wp.hooks.addFilter(
'editor.BlockEdit',
'ta/image-block-photographer',
withInspectorControls
); 3) Save the attribute valuefunction addBackgroundColorStyle( props, blockType, attributes ) {
const { showPhotographer = false } = attributes;
if(blockType.name != 'core/image'){
return props;
}
return assign( props, { showPhotographer: showPhotographer } );
}
wp.hooks.addFilter(
'blocks.getSaveContent.extraProps',
'ta/save-image-block-photoghapher-attr',
addBackgroundColorStyle
); 4) Filter the block render in PHPfunction ta_extend_image_block($block_content, $block){
// If block is `core/image` we add new content related to the new attribute
if ( $block['blockName'] === 'core/image' ){
$attrs = $block['attrs'];
if( isset($attrs['showPhotographer']) && $attrs['showPhotographer'] )
$block_content .= "Has photographer :O";
}
return $block_content;
}
add_filter( 'render_block', 'ta_extend_image_block', 10, 2); |
Describe the bug
I extended the Gutenberg Button block, in the save function I used
blocks.getSaveContent.extraProps
and added a custom data attribute. When a non Super Admin user publishes this button, the data attribute is stripped out. - It works fine for Super Admin permissions.The
blocks.getSaveContent.extraProps
filter is described as:This filter is used to add extra props to the root element of the save function. For example: to add a className, an id, or any valid prop for this element.
The any valid prop doesn't seem to include data attributes. Even if defined in the
wp_kses_allowed_html
as valid.Background
I followed Jürg Hunziker's post on how to extend a WP block.
My block of choice was the button block, where I added a text field called "telemetry name" to the inspector controls.
My hopes was to use that the telemetry name entered by the user and add it to a data attribute on the button itself. However, the button seems to be tossing out my data attribute. Even after adding my data attribute with the
wp_kses_allowed_html
hook.To reproduce
Steps to reproduce the behavior:
blocks.getSaveContent.extraProps
filter, you add a data attribute, and not just adding classeswp_kses_allowed_html
and define your custom attribute eg)$tags['div']['data-telemetry-name'] = true;
Expected behavior
My expectation is that Editor level users should be able to save the extended block and the block should respect the
wp_kses_allowed_html
(like it would for adding an iframe - which works fine)Code
index.js
of your block extension./components/inspector.js
./components/save.js
styles are defined as
./styles/editor.scss
and./styles/style.scss
but are blankin an
index.php
or some file used in the pluginAdditional context
Gutenberg version: 5.8.0
WP version: 5.2.1
The text was updated successfully, but these errors were encountered: