In this tutorial, you will learn how to modify the default settings of the post excerpt block in WordPress through code.
WordPress native blocks offer initial settings that, while they can be adjusted from the block editor, often require repetitive modifications to suit your needs.
For example, you might want to change the default length of the excerpt or the text of the “Read more” link, without having to manually adjust it every time you add this block. Below, I show you how you can make these changes by default.
Code to customize the default attributes of the post excerpt block
Add the following snippet to the functions.php file or your functionality plugin:
To see this and another 1091 code snippets of this website, login or subscribe here.
This code uses the block_type_metadata
filter to customize the default settings of the post extract block. The changes you make are as follows:
showMoreOnNewLine
: Iftrue
, displays the “Read more” link on a new line. When set tofalse
, the link is displayed at the end of the excerpt.excerptLength
: Defines the length of the excerpt. In this case, it is set to 90 words.moreText
: Customize the link text to read the rest of the content. Here you set it by default as “Read more”.
With these modifications, every time you add a post excerpt block, these settings will be applied automatically. Of course, if you need to make specific settings for a post, you can modify them individually from the block editor.
Conclusion
Thanks to the use of the block_type_metadata
filter, you can define the default settings of the post extract block, which allows you to save time and maintain consistency in the presentation of your posts.
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. 🙂