Discover in this tutorial how to customize the default settings of the gallery block using code.
WordPress native blocks come with default settings. That is, when you enter the block, they already have the settings of the block and, although you can change them from the block editor, it is possible that in some of them you will always have to modify some setting.
For example, the gallery block crops the images by default, but you can tell it not to do so and thus avoid having to change that setting every time you add a new gallery block. And for the same price you can set the default link or size.
In this tutorial we will see how you can do that.
Code to customize the default attributes of the gallery block
Add the following code at the end of functions.php or in your functionality plugin:
In this snippet you use the
block_type_metadata filter to customize the default gallery block settings.
In the attributes you indicate:
cropsthe image, if
falseit does not.
linkTo: indicate the destination of the image
sizeSlug: specify the size of the image
In the example above you get the following:
- That the images are not cropped.
- That the images are not linked, so you can use the native WordPress lightbox.
- That the default size of the image is the medium size and that the image opens in a new page.
Now every time you add a gallery it will have these settings selected.
And if in any case you want to modify them, you will be able to do it without any problem.
Thanks to the
block_type_metadata filter you can decide which are the default settings of the gallery block and save a lot of time when entering the block.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂