Why do WordPress’ block templates no longer use PHP and what does it mean for building themes and sites
In a classic theme, templates are PHP files where we can use WordPress’ predefined functions and our own together to get, manipulate, and show data. In a block theme, templates are HTML files, so we can not add PHP functions.
For example, dynamically adding the current year to the footer of a classic theme is a straightforward process.
? <?php echo date("Y"); ?> Company
It is ready. From now on, we do not have to change it anymore, as this will always show the current year.
Doing the same in a block theme?is not that simple.
Locating the template is very similar. And we do not need to edit the file directly, we can use WordPress editor. However, we still can not add a PHP function there.
Does this mean we can no longer use PHP in our templates? Not at all, we just need to?do it in the right place.
The proper way to do it is?with a plugin.
There are other inappropriate ways to do it.
All of these methods make the same mistake of placing?functionality that is not related to design and presentation in the theme. This has been a requirement for submitting a theme to WordPress.org for years, meaning this is not how it is supposed to be done.
领英推荐
Why is that? Mostly because it breaks the integrity of the whole system. It may sound pretentious, but it means that by not using WordPress the way it is designed, we are creating new problems.
The most important is that if we activate a different theme on the site, the code does not work, because we included it in a theme, so it works as long as that theme is active.
So far, I have written about consistency in the system, and how important it is to separate design and presentation from other functionality. But one question remains unanswered.
Why do WordPress’ block templates no longer use PHP? What is the reason behind it? The short answer is?states.
The block editor is the application behind blocks in WordPress. It is since version 5.0 integrated as part of WordPress core, but before that, it had to be installed as a plugin named Gutenberg.
It uses a Javascript library called React, and on top of that, it uses @wordpress/data, its version of a library used to manage states called Redux.
What exactly is a state? Let me try with an analogy.
Picture an application as a theatre play. It has a script, which is the code. And also actors, which are the components. For classical theatre, it works. Each actor knows their part and plays accordingly.
Modern applications are more like improvisational theatre. Each actor may have a foundation for their character, but they must react — no pun intended — and build upon the actions of the other members of the cast to move the story forward. The state is the story, that is not written, but can be kept and built upon for the duration of the play.
States are a very effective way to keep track of changes in components, allowing the block editor to reflect instantly the changes we make in any block: content, position, attributes… and outperforming PHP in the front.
If the price we must pay in exchange is not being able to use quick fixes, I think it is a fair price.
CEO Evermight
1 年Great post! I know how to make short codes through a plugin and then use the short code block. Can you clarify what you mean by using plugin hooks to add dynamic content to a block? Either with pseudo code or link to a blog/article that elaborates more on this process? And for the last option, which is to create a dynamic block that can be used by the editor, that's basically the React.JS approach you talk about in the 2nd half of your article right? For which there's now plenty of official training guides on the wordpress website...