Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Add a Custom Database Error Page in WordPress

Have you ever seen the ‘Error establishing a database connection’ error page on your WordPress site? This error message can be displayed for various reasons, but it’s unattractive and may confuse your visitors.

At WPBeginner, we’ve found that creating a custom database error page is incredibly beneficial. It presents a more professional image to your users, reassuring them that you’re aware of the issue.

Plus, it provides an opportunity to offer helpful resources or alternative ways to access your content while the problem is being resolved.

In this article, we’ll show you how to customize your database error page in WordPress.

Another problem with this error is that you won’t realize that your website is down until you visit it and see the message. So, we’ll also show you how to set up a notification for each time your website goes down due to a database error.

How to Add a Custom Database Error Page in WordPress

What Causes Error Establishing a Database Connection?

‘Error establishing a database connection’ is one of the most common WordPress errors. It can be caused by incorrect database information in your WordPress settings, a corrupt database, or an irresponsive database server.

Database Connection Error in WordPress

Solving this error can be tricky for beginners, so we’ve listed a number of solutions to the error in our guide on how to fix the ‘Error establishing a database connection’ in WordPress.

The last thing your visitors want to see is a generic error message displayed by WordPress. The error page lacks details, is unattractive, and doesn’t contain any of your branding. It just looks like your website has vanished.

With that being said, let’s take a look at how you can customize the error page on your WordPress website to make it more helpful.

Adding a Custom Database Error Page in WordPress

First, you need to open a new file in a text editor such as Notepad, and then paste the following content inside.

<?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>

Source: CSS Tricks

Next, save the file as ‘db-error.php’. Then, using an FTP program, you need to upload the file to your WordPress site’s /wp-content/ directory. For more details, refer to our beginner’s guide on using FTP with WordPress.

Now when your website is unable to establish a database connection, you will see the error message in the screenshot below, and the title “Database Error” will be displayed in the tab of the web browser.

Database Error Page Preview

It’s not the most attractive error message, but it’s a step forward. Now you can change the page’s HTML and CSS to better suit your own website.

Customizing the Database Error Page in WordPress

Now it’s time to customize your database error page so it looks great and matches the theme of your website. You should also add your brand logo and tagline to the page.

You could also use a little humor in your message. Your visitors may not be able to visit your website, but you can at least keep them amused.

You can take inspiration from our list of the best WordPress 404 error page design examples. For example, this Empty Cup 404 page could be easily modified to serve as a database error page.

Empty Cup 404 Error Message Example

You would need to change ‘Error 404’ to ‘Database Error’ and ‘This page can not be found’ to ‘This website is temporarily unavailable’. You should also remove the ‘Go Home’ button since it’s likely your entire website is down.

The easiest way to create a custom database error page design is to use the SeedProd plugin. It is the most popular landing page builder with drag and drop functionality to customize your error pages without editing any code.

SeedProd

It comes with 300+ professionally-designed templates, including many different 404 error page templates that you can use as for your custom database error page.

There is a free version of SeedProd, but you’ll need the Pro version to access the built-in 404 error page templates.

For ideas on how to use SeedProd to create custom error pages, check our guide on how to improve your 404 page template in WordPress.

Sending an Email Notification for the Database Error

If your website is down, then you want to be notified so you can do something about it.

The code snippet we used earlier contains code that will send you an email notification whenever your website has a database error. That part of the code is currently inactive since it is commented out by two slashes.

// 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");

To receive the notifications you will need to remove the two slashes before the mail function.

// 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");

You’ll also have to replace ‘your@email.com’ with your own email address. Don’t forget to save the edited db-error.php file and upload it to your WordPress site’s /wp-content/ directory.

We hope this tutorial helped you learn how to add a custom database error page in WordPress. You may also want to learn about common WordPress block editor problems and how to fix them or check out our list of must have WordPress plugins to grow your site.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

6 CommentsLeave a Reply

  1. Jiří Vaněk

    We occasionally experienced MySQL server outages on our website, and it looked really unprofessional when WordPress displayed the default error message. This is a great solution for when the MySQL server goes down again. I can style the page in HTML to match the website’s colors and turn the classic error into something that looks like maintenance mode. I hope we’ve fixed the MySQL server issue, but if not, this article has inspired me on how to handle it more professionally so it doesn’t look like something is broken. This is great.

  2. Shyam Chathuranga

    Thanks for this article. I’m using this with another piece of code to track how many times visitors to my site see these kinds of DB Errors and PHP Fatal Errors via Google Analytics. For anyone wondering how they can create a custom PHP Fatal Error page, just copy paste the db-error.php and rename it to php-error.php.

    I noticed my site monitoring tools some times don’t alert me on these errors maybe because of page caching. I also enabled Email alerts from the code you guys provided. Works great!

    Thanks so much!
    Shyam

    • WPBeginner Support

      Thanks for sharing and glad our article was helpful!

      Admin

  3. Kensley

    This tutorial came just when I needed it, and is one of the things we can actually do at the file level when attempting to access the WP dashboard is impossible.

    Much appreciated!

  4. Arlen Nagata

    Now, is there an action to put this in your theme folder?

  5. David

    You know, I never thought about customizing that page. It’s a good idea because if a visitor comes across it at least it’ll fit the theme of your site and while they can’t see any info, maybe they can be amused.

    The email seems like a good idea because it could help gauge how often visitors might hit that error. If it happens a lot then Houston we have a problem.

    My only question goes back to the email. If a user lands on that page, I get an email. Will I get another email if that user reloads the page? Say the visitor lands, reloads in 10 seconds and continues. I don’t want to be spanned. Also, is the email hidden in the source code?

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.