Discover with this tutorial how to display the current year dynamically on any website thanks to the Block Binding API.
I’m sure you’ve seen more than once a website that displays the copyright in the footer but with the year a bit out of date, right?
This is relatively easy to fix in classic themes as you can add some PHP to make the year update dynamically. In block themes, on the other hand, it was a bit more complex…. until now!
Thanks to a couple of new features in WordPress 6.5 it’s much simpler now. The first new feature is the new API called Block Binding. This provides the option to display custom fields directly in a block. This is fantastic news as now you don’t need to create a custom block but can use an existing one.
Currently there are only 4 blocks that allow you to do this:
- Paragraph
- Heading
- Button
- Image
This already allows you to do very interesting things and I’m sure that in future WordPress updates this feature will be extended to more blocks.
This, along with the new filter that allows you to register variations using PHP (previously you could only use JavaScript to do so), makes it much easier to use a native block that displays a dynamic data directly from the editor.
In this case, you are going to create a variation of the paragraph block that displays the current year. Let’s see how.
Steps to display the current year dynamically in a native block
1. Add a source that can get the current year via block binding and create a block variation
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
With this code you first add a new data source for the Block Binding and then use the get_block_type_variations
filter to create a variation of the paragraph block that displays the current year.
Now there is only one step left.
2. Insert the block where you want
WordPress 6.5 does not have a visual interface to manage the block binding, but since you have created a block variation you will be able to access it directly:
Once you enter it you will see something like this:
The advantage of having the current year in a block, which also updates automatically, is that you can layout it as you like.
For example, if you want to add text before (or after) you can use the row block (a variation of the group block).
Conclusions
You can see how easy it is to display the current year dynamically at any point of the web thanks to being able to enter it directly in a paragraph block.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉