It’s technically possible, but probably too complex/nuanced.
This will add the data-pin-description and data-pin-title values for core/image blocks by default.
add_action('enqueue_block_editor_assets', function() {
$js = <<<JS
wp.hooks.addFilter('blocks.registerBlockType', 'attributes-for-blocks/defaults', settings => {
if(settings.name === 'core/image') {
return {
...settings,
attributes: {
...settings.attributes,
attributesForBlocks: {
...settings.attributesForBlocks,
default: {
'data-pin-description': '',
'data-pin-title': '',
},
},
}
}
}
return settings
});
JS;
wp_add_inline_script('attributes-for-blocks', $js);
});
It will invalidate any existing blocks that don’t have custom attributes as it now has a different default value.
Thread Starter
axa222
(@axa222)
Thank you for answering so fast!
Please, forgive me but I don’t understand where exactly should your code go.
I mean, should it be added to one of your existing plugin files or to one of the core WP files?
Thank you!
Thread Starter
axa222
(@axa222)
Got it working! Thanks!!!!
Thread Starter
axa222
(@axa222)
Is it possible to add these attributes to the IMG tags – your suggested code adds attributes to FIGURE tags.
Thank you!
-
This reply was modified 3 years, 2 months ago by
axa222.
should it be added to one of your existing plugin files or to one of the core WP files
Your child theme’s functions.php is the preferred location. If you add to plugin or WP core files the changes get overwritten on updates.
Is it possible to add these attributes to the IMG tags – your suggested code adds attributes to FIGURE tags
No, the plugin can only add attributes to the root element blocks.
I may add these features you’re looking for in the future, but currently I don’t have the time as I’m working on something else, sorry.