When developing WordPress blocks, you'll also need to consider the functionality of your block
and whether it needs to change based on external factors.
Fortunately, it is possible to create blocks that are either static or dynamic,
depending on your requirements. Let's look at what the difference is between static and dynamic blocks,
how to determine which is right for your needs, and the different approaches for development.
If you've been following the lessons in this module to build the copyright date block,
you've been building a static block. A static block's content is fixed. Once it's added to
the editor and the save function is triggered and the post or page is saved, the block's content
will not change. Static blocks are useful for content that doesn't need to change, like a quote
or a testimonial. However, if you consider the real-world requirements of a copyright date block,
it would actually be ideal that if the physical year changes, the rendered content of the block
should also update. Otherwise, you'll need to go and edit everywhere that you've added the block
to trigger the save function and update the year. This is where dynamic blocks come in.
Dynamic blocks do not render their content via the save function and instead use PHP to render
their content when a front-end request is made for a postal page that includes the block.
Let's look at what it would take to turn the copyright date block into a dynamic block.
To make the copyright date block dynamic, you need to specify a PHP file or function
that contains the block's rendering logic. This can be done in a few ways, but the easiest is to
use the render property of the block's metadata in the block.json file. Open the block.json file
and add the following code to the bottom of that file. This tells WordPress to use a file with a
file name render.php to render the block's content on the front-end. You can then create a render.php
file in the source directory and add your rendering logic to that file.
First, make sure to open the PHP tags. Then you can use the get block wrapper attributes
function to get the block wrapper's attributes. This is similar to calling use block props in
JavaScript. You can then get the starting year value from a PHP attributes array.
This attributes variable is one of three that are exposed to the file you set for your block's
render metadata property and contains any attributes that have been set up for your block.
You can then create the current year variable.
This uses the PHP date function to always get the current year. This way, when the block is
rendered, it always gets the current year. Last but not least, output the paragraph tag,
including all the relevant properties and content. First, we will close the PHP tags
and then we can create the two opening and closing paragraph tags.
We can start putting in the copyright content with a copy symbol and then we can use PHP tags to echo
the starting year, the dash, and PHP echo the current year.
And then to ensure that the block properties are applied to the paragraph tag, we can echo
the block props variable.
Inside of the paragraph tag.
Once you set up your render dot PHP file, you can remove any code related to the block save process
as you won't need to save any content to the post. To do that, you can delete the save dot JS file.
You can also delete the save property pass to register block type in the index.js file.
As well as the import that imports the save function.
Once you've made those changes, you can run the bold process and then create a post and add the
block to the post. You'll see that the block still renders as expected in the editor. However,
if you view the block code in the code editor,
you'll see that the saved version does not include any rendered output. When you preview it,
the content is rendered using the PHP render file.
If you simulate the year change, say by changing the value of the variable in PHP,
the front end rendering of the block will change.
For more information on the difference between developing static versus dynamic blocks,
you can read the static or dynamic rendering of a block section of the fundamentals of block
development in the block editor handbook.