Customize WordPress Database Error Page

When doing some research for the new version of my Fiber Admin WordPress plugin, I found that in the core code of WordPress, they allow us to override the default Database Error Page. It will tell the whole world that you’re using WordPress and your website is having some problems with the database connection 👎.

Default WordPress Database Error Message

Luckily, WordPress has feature to allow us customize this default page. Let’s give it a try!

Follow the instruction I found with CSS Tricks here. I found that we can customize the DB Error page by a file called db-error.php in wp-content. But why this file?

In wp-includes/functions.php, WordPress has a function called dead_db which you can see they will allow us to load custom template for the Database Error Page, the custom template will be named with wp-content/db-error.php.

WordPress function dead_db

With db-error.php, we can add inline CSS and customize the content of this page so it will look different from the default DB Error Page which is applied to all the other WordPress pages.

Some tips I found:

  • You may want to put your logo image on this page so the users will know exactly which error message is belong to.
  • Make sure to include the error code like 503 on this page. It’s for DB error message, so the error code should be 503. It will help you know exactly what happened with your site.
  • Some security plugins can prevent displaying the db-error.php file. For example, Sucuri Hardening option can prevent this custom file display because they harden all the PHP files inside wp-content folder. You may need to add extra rules to exclude the db-error.php file.

Add plugin requirements to WordPress theme without breaking the site

After deciding to separate framework into a plugin for inhouse dev, I need to implement the feature to force my theme require framework plugin installed and activated. Since I use many helper functions in Framework, so the theme need Framework plugin to run without any issues.

It’s the solution how I can make my theme requires Framework plugin. I don’t think it’s the best solution but it works in my case (may be I will update the better solution in the future).

What I need will be:

  • Show notice in admin if the Framework plugin is not activated.
  • Make plugin requirement without any issues if the plugin haven’t been installed yet.

This is the final code I added to the very first line of my theme functions:

It will stop functions.php at the line checking for plugin active, so the theme doesn’t run the other code (with unknown Framework helper functions) and doesn’t break the site also.

Update equeued file sources in WordPress

Today I have a problem when building obfuscation code for my team. After creating the uglify files, I need to replace all the original file sources with the uglify ones. The most important thing is I want the code still works with WP Rocket plugin.

After a quick research, I found that WordPress has many hooks to adjust the assets.

First of all, I think I need to list all the scripts and styles enqueued, so I found this link. It’s pretty cool that I can get all the things enqueued, but I can’t change the enqueued resources with this hook because it’s called when the resources already enqueued.

Because I just want to get all the enqueued scripts, so I tried with the filter script_loader_src which I’ve used to remove query string from some core WordPress scripts before. I think it’s the correct filter I need, it has the $source variable for me to update, and the $handle to filter out which scripts I need to replace. Cool! Let’s do the code!

The main purpose of this code is getting all the enqueued resources, filter out which has my theme prefix in the $handle to update the resource URL. The final code will be something like this:

That’s it 🎉