How to Add a Custom Database Error Page in WordPress

custom database error page

How to Add Custom Database Error Page in WordPress and why is explained in this article.

After syntax and 404 error, database connection error is the most frequent error you are likely to see on your WordPress site.

There are various reasons for its occurrence and unlike 404 error, you may not know about the web pages showing database connection error unless someone reports you.

As a matter of fact, database connection error can make your entire website down.

The user will down the error page which looks quite ugly and if they happen to see very often, they are likely to stop visiting your website and unsubscribe you.

In such a scenario, it is important to add a custom database error page in WordPress.

It is also important from your side to get a notification every time your website gets down with database connection error so that you can approach your technical team or web host to fix the issue immediately and for the time being, you can run your website from another backup server if you have one.

One of the common reasons for database connectivity error is when you migrate your website from one web server to another.

WordPress interact with MySQL database through the scripting files and once the connection is established and data are fetched, the webpage appears for the users.

If there is any problem, your user will see “Error Establishing a database connection” and the script from the file.

error establishing database connection

Why Should You Add A Custom Database Error Page In WordPress?

There is always a big chance that the script displayed due to database connectivity error could help the attackers to find loopholes.

It is generally not the case but there is a possibility. Therefore, you should never risk it and show a custom database error page where no script or details are displayed.

Furthermore, your normal users will be completely pissed to see an ugly database error page shown by WordPress.

He can unsubscribe your newsletter, notifications and unbookmark you.

Why piss them off when you can show something else to keep them engaged and promise them to comeback soon.

Moreover, every time your custom database error page is used in WordPress, you will be notified and you can take actions immediately. In this article, we will show you how to add a custom database error page in WordPress.

Steps To Add A Custom Database Error Page In WordPress –

If you are wondering how it works, you should know that it works automatically.

In other words, whenever WordPress fails to connect to the database, your custom database page will be displayed automatically.

If your custom database error page is not available, it will show the default ugly one.

Step 1. You have to create a file and name it db-error.php. You have to paste the following code in it.

<?php // custom WordPress database error page
  header('HTTP/1.1 503 Service Temporarily Unavailable');
  header('Status: 503 Service Temporarily Unavailable');
  header('Retry-After: 600'); // 1 hour = 3600 seconds
  // If you wish to email yourself upon an error
  // mail("your@email.com", "Database Error", "There is a problem with the database!", "From: Db Error Watching");
?>

<!DOCTYPE HTML>
<html>
<head>
<title>Database Error</title>
<style>
body { padding: 20px; background: red; color: white; font-size: 60px; }
</style>
</head>
<body>
  You got problems.
</body>
</html>

If you want notification to your email every time this file gets executed on your server which in other words mean that you have database connectivity error on your website, you need to remove the comment symbol about the mail function written above.

custom database error message

Step 2. You have to connect your FTP to your web server and inside wp-content, you have to place the file. That is it and you can customize your custom database error page as per your liking to make it interesting and creative.

Here is another example of lines of code that you can be used instead of the above-mentioned one.

<?php // custom WordPress database error page tutorial @ digwp.com

    header('HTTP/1.1 503 Service Temporarily Unavailable');
    header('Status: 503 Service Temporarily Unavailable');
    header('Retry-After: 3600'); // 1 hour = 3600 seconds
    mail("spamless@domain.tld", "Database Error", "There is a problem with teh database!", "From: Montgomery Scott");

?>
<!DOCTYPE HTML>
<html dir="ltr" lang="en-US">
    <head>
        <title>503 Service Temporarily Unavailable</title>
        <style type="text/css">
            h1, p {
                font-family: Helvetica, sans-serif;
                font-size: 24px;
                color: #333;
                }
            p {
                font-size: 14px;
                }
        </style>
    </head>
    <body>
        <h1>Captain, the ship can&rsquo;t take much more of this!</h1>
        <p>Hard workers are currently working on the issue &mdash; Please check back soon!</p>
    </body>
</html>

This is how you can show funny messages so that the users do not get users and eliminate you. If you know CSS coding, you should make it more visually appealing so that it means something rather than just unappealing plain white background.

If you see the code, there are three different headers present. The first and second one is 503 server and status response which will ask the users to try back. But the third one Retry-After will show the users after how much time, they should try back.

Tips On How To Add A Custom Database Error Page In WordPress?

1. You should definitely add your brand logo and tagline to make it look good.

2. You can also add URLs of some of the best articles on your other website or some static pages you have on your website that are not dependent on database connectivity. You can also paste the link of your social media channels so that your loyal visitors that check them out to see the updates when it comes live.

3. You can cover up by not displaying it as a database error and say that it is down for some upgrades.

4. If the issue will take some time to resolve, you can forward an email to your subscribers regretting the fact and notifying them when it is likely to come back.

seedprod coming soon

Lastly, there are various custom database page templates available for free on the internet that are very amusing, interesting and attractive.

Download the best one from them and use the same of your site to amuse your visitors while your site is down due to database connectivity error.

Thank you for visiting SKT Themes a place for selecting top WordPress Themes from a list of several hundred templates. Happy Reading and Shopping.