Modifying existing blocks in Gutenberg may seem complex, but with WordPress, there are various JavaScript functions for adjusting supports, styles, adding HTML classes, and more. In this article, we’ll show you how to disable certain block supports to make your site even more personalized.
Pour commencer, vous devez créer un fichier editor.js
à la racine de votre thème. Ce fichier contiendra le code JavaScript nécessaire pour modifier les supports des blocs. Ensuite, vous devez inclure ce fichier dans votre thème en ajoutant le code suivant à votre fichier functions.php
:
This code ensures that the editor.js
file is loaded into the WordPress block editor, allowing you to modify existing blocks.
Deactivate existing block supports
You can disable support for each block by adding the following code to your editor.js
file:
This JavaScript script modifies block parameters to disable support for sizing properties (padding, margin and gap) for buttons (core/buttons
for parent and core/button
for sub-blocks).
Detailed code explanations
- Function
disableBlockSupports
: This function takes two arguments,settings
andname
.settings
contains the block parameters, andname
is the block name. - Disabling Dimensions: If the block name is
core/buttons
, we disable the spacing support (spacing
). If the block contains inner blocks (innerBlocks
), we iterate through each inner block to also disable spacing for blocks of typecore/button
. - Block Registration Filter: We add a filter to
blocks.registerBlockType
that applies ourdisableBlockSupports
function whenever a block is registered. This ensures our modification is applied to all blocks of typecore/buttons
andcore/button
.
With these adjustments, you can customize Gutenberg block supports to better suit the needs of your WordPress site. Test these modifications to see how they can improve the appearance and functionality of your blocks!