1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WordPressでサイトを停止せずにメンテナンスモードにする

Last updated at Posted at 2019-09-27

WordPressでサイトをメンテナンスしたい場合、

  • アクセスして来たユーザーに対しては、メンテナンスのメッセージを表示する。
  • 自分は、いつも通りWordPressの管理画面/フロント画面を操作・表示できる。

という感じにする方法を紹介します。

メンテナンスモードにするプラグインが存在しており、それを使用するのも良いですが、使う機会が限られているプラグインをインストールしたままというのは、無駄な気もします。

functions.phpに以下のコードを追記するだけで、メンテナンスモードに出来ます。

/* メンテナンス表示 */
function maintenance_mode() {
    if (!current_user_can('edit_themes') || !is_user_logged_in()) {
        wp_die('ただいまメンテナンス中です。');
    }
}
add_action('get_header', 'maintenance_mode');

get_headerにフックをかけ、テンプレートを読み込む直前に関数「maintenance_mode」が実行され、
WordPressにログインしていない場合、メンテナンスモードの表示を行います。
WordPressにログインしていれば、通常通り、操作と表示が行えます。

メンテナンスが終了したら、以下のコードをコメントアウトします。

// add_action('get_header', 'maintenance_mode');

以上となります。

WordPressの管理者は大抵ログアウトすることはせず、常にログイン状態であることが多いので、上記のコメントアウトをするということを忘れていると、自分はいつも通り表示出来ているが、他人からはいつまでもメンテナンス中と思われますので、その点ご注意ください(笑)

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?