TwentyTwentyTwo – How to add PHP logic to Post templates?

I’m testing out the new TwentyTwentyTwo theme for WordPress 5.9.
I can see that when I enter a post, on the right side I have the option to either Edit or Create New Templates.

enter image description here

In edit mode, it brings us into the edit view of that template (which kinda reminds me of a reusable block in a way..)

enter image description here

But how do we “edit and save” PHP code for these templates? Like loops, actions, do_actions, etc… I don’t see the templates being auto generated in my Visual Studio Code environment anywhere….

1 Answer
1

But how do we “edit and save” PHP code for these templates? Like loops, actions, do_actions, etc… I don’t see the templates being auto generated in my Visual Studio Code environment anywhere….

You don’t, these are block templates, not PHP templates. When in the filesystem in a theme they are plain .html files, and when you modify or create them in the user interface they’re saved in the database.

In order to run PHP code in these templates you would need one of the following:

  • a custom block that renders in PHP
  • a custom shortcode inserted via a shortcode block

This is the same as asking how to run PHP inside a posts content, and the solutions are the same. Custom blocks and shortcodes.

Leave a Comment