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

WordPressでカスタムデータベースエラーページを追加する方法

編集メモ: WPBeginner のパートナーリンクから手数料を得ています。手数料は編集者の意見や評価に影響を与えません。編集プロセスについて詳しく知る。

WordPressサイトで「Error establishing a database connection(データベース接続の確立エラー)」というエラーページを見たことがありますか?

このメッセージはさまざまな理由で表示されますが、魅力がなく、訪問者を混乱させる可能性があります。また、サイトを訪問してメッセージを見るまで、サイトがダウンしていることに気づかないでしょう。

この投稿では、WordPressのデータベースエラーページをカスタマイズする方法を紹介します。また、データベースエラーでサイトがダウンするたびに通知を設定する方法も紹介します。

How to Add a Custom Database Error Page in WordPress

データベース接続の確立エラーの原因は?

Error establishing a database connection(データベース接続の確立エラー)」は、WordPressで最もよく見られるエラーのひとつです。WordPressの設定に間違ったデータベース情報が含まれていたり、データベースが破損していたり、データベースサーバーが応答しなかったりすることが原因です。

Database Connection Error in WordPress

このエラーを解決するのは初心者には難しいかもしれません。そこで、WordPressの「Error establishing a database connection(データベース接続の確立エラー)」を修正する方法のガイドで、エラーの解決方法を番号付きリストでご紹介します。

訪問者が一番見たくないのは、WordPressが表示する一般的なエラーメッセージです。エラーページには詳細がなく、魅力的でもなく、あなたのブランディングも含まれていません。あなたのサイトが消えてしまったように見えるだけです。

そこで、WordPressサイトのエラーページをより使いやすくカスタマイズする方法を見てみましょう。

WordPressにカスタムデータベースエラーページを追加する

まず、メモ帳などのテキストエディターで新規ファイルを開き、以下のコンテンツを貼り付ける。

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

ソースCSSトリック

次に、ファイルを「db-error.php」として保存します。次に、FTPプログラムを使用して、WordPressサイトの/wp-content/ディレクトリにファイルをアップロードする必要があります。詳細については、WordPressでFTPを使用するための初心者ガイドを参照してください。

あなたのサイトがデータベース接続を確立できない場合、下のスクリーンショットのようなエラーメッセージが表示され、ウェブブラウザーのタブに “Database Error “というタイトルが表示されます。

Database Error Page Preview

魅力的なエラーメッセージではないが、一歩前進だ。これで、ページのHTMLとCSSを自分のサイトに合ったものに変更することができる。

WordPressのデータベースエラーページのカスタマイズ

データベースエラーページをカスタマイズして、サイトのテーマと一致するようにしましょう。また、ページにブランドロゴとキャッチフレーズを追加してください。

メッセージにちょっとしたユーモアを盛り込むのもいい。訪問者はあなたのサイトを訪れることはできないかもしれないが、少なくとも彼らを楽しませることはできる。

WordPressの404エラーページのデザイン例からインスピレーションを得ることができます。例えば、このEmpty Cupの404ページは、データベースエラーのページとして簡単に修正できます。

Empty Cup 404 Error Message Example

エラー404」を「データベースエラー」に、「このページが見つかりません」を「このサイトは一時的に利用できません」に変更する必要があります。また、サイト全体がダウンしている可能性が高いので、’Go Home’ボタンも削除する必要があります。

カスタムデータベースエラーページのデザインを作成する最も簡単な方法は、SeedProdプラグインを使用することです。これは、コードを編集することなくエラーページをカスタマイズするためのドラッグ&ドロップ機能を備えた最も人気のあるランディングページビルダーです。

SeedProd

300以上のプロがデザインしたテンプレートが付属しており、その中にはカスタムデータベースエラーページとして使用できる様々な404エラーページテンプレートも含まれています。

SeedProdには無料版がありますが、ビルトイン404エラーページテンプレートにアクセスするにはPro版が必要です。

SeedProdを使用してカスタムエラーページを作成する方法については、WordPressの404ページテンプレートを改善する方法のガイドをご覧ください。

データベースエラー通知メールの送信

もしサイトがダウンしていたら、そのことを知らせてほしい。

先ほど使用したコード・スニペットには、サイトにデータベース・エラーが発生したときにメール通知を送信するコードが含まれています。この部分はスラッシュ2つでコメントアウトされているため、現在は無効になっています。

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

通知を受け取るには、メール機能の前にある2つのスラッシュを削除する必要があります。

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

また、’your@email.com’ を自分のメール・アドレスに置き換えてください。編集したdb-error.phpファイルを保存し、WordPressサイトの/wp-content/ディレクトリにアップロードすることを忘れないでください。

このチュートリアルで、WordPressでカスタマイザーエラーページを追加する方法を学んでいただけたでしょうか。また、WordPressのブロックエディターでよくある問題とその解決方法について学んだり、サイトを成長させるために必須のWordPressプラグインのリストをチェックしたりすることもできます。

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.

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

Avatar

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.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

6件のコメント返信を残す

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Shyam Chathuranga says

    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

  3. Kensley says

    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. David says

    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?

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。