Find out in this tutorial how to detect if an installation has an active block theme and make sure to run a function (or any code) only on websites that use that type of theme.
For the next few years block themes (FSE) will coexist with classic themes (with .php templates).
This is not a problem, but if you are creating a plugin or custom code and you want to run only if a specific type of theme you must take this into account to avoid causing errors in installations with a version of WordPress that have the other type of themes.
For example, if you want to interact with header elements such as logo, site description, navigation menus…, it will be very different if the theme is classic or blocks, since in the latter all of them will be blocks.
So it is advisable to check if there is an active block theme where the code will be executed and use a conditional to do it only if that is the case.
In this tutorial you will learn how to do it
Code to execute different code depending on the type of theme
To detect if there is an active block theme you can use the
This would be a simple example of how to display the WordPress version using a shortcode.
Add the following code at the end of functions.php or in your functionality plugin:
This is a simple example where you detect if it is a block theme or not and it shows you. But this is not the important part.
The idea is to use this information to make the execution of a code conditioned by the type of theme of the site in question.
The function could be something like this:
As you can see, you first detect if there is an active block theme and then execute one code or the other conditionally.
To adapt these examples to your convenience you only have to add the code that you want to be executed depending on the type of theme.
Now you know how to detect if a WordPress installation has a block theme active (or not) and run different codes depending on the type of theme.
Any questions? Let me know in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉