Blog /

How to kill the website when theme is missing or broken

A short snippet that will detect if the active theme is the theme you expect. If not or it’s broken, the website will die and your data won’t leak.

You might easily imagine the following situation. You have a tailor-made website with tons of custom functionality and part of them is related to the active theme. Memberships, showing/hiding part of the content based on user role, etc. The very last thing you want is to display raw texts when something goes wrong.

The default WordPress behavior is “smart”. Even though it knows that the theme is broken, it just tries its best and shows the content as it is and just simply doesn’t load styles and functionality. That might be great if you have recipes or sort of public information there, but if you don’t, you won’t be happy at all.

For that case, I’ve written a tiny snippet that you could upload to mu-plugins folder, which means that nobody can’t turn it of accidentally.

* Plugin Name: Theme Guard
* Plugin URI:
* Description: Check if theme exists and is active or die.
* Version: 0.0.1
* Author: Karolína Vyskočilová
* Author URI:
* License: MIT
add_action('init', function () {
$current = wp_get_theme();
$web = wp_get_theme('twentytwenty');
if (!$web->exists() || $current->name !== $web->name) {
wp_die('An error occured. We\'ll fix it ASAP.');

view raw


hosted with ❤ by GitHub

Go ahead and modify what you need – check if a plugin is active or if the user is logged in as an admin who tries to debug.

Did you use the snippet somewhere? Let me know in the comments or tag me on Twitter @vyskoczilova.