Wypo Logo Header@2x
Blog / Developer / Gutenberg / Gutenberg: Modifying block variants

Gutenberg: Modifying block variants

Developer Gutenberg
Time to read : 5 minutes

Publication date: 26 June 2024

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 :

PHP

This code ensures that the editor.js file is loaded into the WordPress block editor, allowing you to modify existing blocks.

Delete existing styles

You can delete the styles for each block using the following code in your editor.js file:

JS

In this example, we remove the styles for core/buttons. This can be useful if you want to simplify the options available to users or impose a more consistent style on your site.

Adding block styles

You can add styles for each block using the following code in your editor.js file:

JS

In this example, we’re adding three new button styles (core/button). The default style will be the blue button (primary-button), and we’re also adding styles for the orange button (secondary-button) and the white button (white-button).

Add CSS code to customize each style

These CSS rules define the appearance of the buttons according to the styles we saved earlier. You can customize the styles to suit the aesthetics of your site.

CSS

Conclusion

Editing block variants in Gutenberg is a powerful way to customize the block editor to meet your site’s specific needs. By following these steps, you can remove unwanted styles and add your own, creating a more consistent and aesthetically pleasing experience for users of your WordPress site. Try out these modifications and see how they can improve the presentation and functionality of your blocks!

Last 30 days : 1
Total : 211