I’m building a new site and I wanted to build my theme from scratch. However, the WordPress ecosystem is moving more towards blocks and blockthemes. As far as I can tell, it is possible to put hooks (
apply_filters()) into a block theme, but it seems like I’d be making a weird hybrid. I don’t want to only use HTML (including CSS and JS) when I build some of the pages; I really want to have places that I can hook PHP commands into. It’s probably more correct to say that I have to use PHP for many of the site features and hooks are a great way to do it.
I haven’t seen any hooks inside of blockthemes, so is there a good convention to follow for putting them in one? Or are blockthemes meant to avoid hooks altogether, in favor of blocks and shortcodes? Let’s say that I wanted to have a banner at the top of the home page, but underneath the header that changes according to the user roles and current date (I’ve done that before by hooking into the top of the theme page). What is the best way to implement that in a block theme?
I’m not the most experienced in block themes, but from what I’ve seen of them so far I feel pretty confident in saying that the functionality you describe should be implemented as a block. But it’s wrong to say that absolutely everything needs to be HTML and JS. There is still a place for PHP.
Let’s say that I wanted to have a banner at the top of the home page,
but underneath the header that changes according to the user roles and
current date (I’ve done that before by hooking into the top of the
In this case the banner should be a custom block that can be added to any block area of a block theme. Twenty Twenty-Two has a Header area that might be appropriate, for example.
Such a block should render some kind of preview in the editor with JS, for performance reasons, but when you register the block you can define it as a dynamic block with a
render_callback where you can use PHP logic to render the contents of the block on the front end. That callback would check the user role or date to determine what it renders.
Regarding hooks, those are definitely still relevant, and are still used for all sorts of things. They’re just not particularly relevant for adding markup into themes, but since there was never any standardisation of hooks for themes they were never a great way to do this to begin with.