Adding PHP code to single template in 2023 edition of WordPress theme

That tutorial is for a classic PHP theme, but 2023 is a modern block theme, it works completely differently. You can’t put PHP in the HTML files in templates folder. You should think of these files as starting content, not PHP files, and any changes you make in the side editor are saved in the database as posts of type wp_template and wp_template_part that override these files.

If you want to insert PHP into a block theme it needs to be done via a block. The easiest route is by writing a short code, then using a shortcode block to insert it into the theme.


As a sidenote, be wary of these types of tutorials. It’s telling you to add a custom field to let you set a post as a sponsored post, which on the surface sounds reasonable. The problem comes when you need to exclude sponsored posts from the homepage, or show a list of sponsored posts in a sidebar. Now you’re forced to write more code to make that happen, and because the data is in a custom field ( post meta ) the performance is awful.

Instead a custom taxonomy, something you can generate code for by filling out a form on a WP generator site, would give you a proper GUI in the editor for free, template files (templates/taxonomy-name.html, archive pages that can show up in google sitemaps, REST API endpoints for javascript etc. This and the performance in a worst case scenario could be 100x faster.

If you need to filter/search/group posts that do or do not have this field then it should be a custom taxonomy. Perhaps a Post Features taxonomy with Sponsored/Featured etc.

Likewise, a category or tag would do the job better than a custom field.