WordPressの簡単なメンテナンス画面とwp_dieのカスタマイズ

  • 公開日:2014/9/26
この記事は最終更新日から2年以上が経過しています。

WordPressでプラグインを使わずに以下のような簡易なメンテナンス画面を表示する方法。

functions.phpに以下を記述します。

function quick_maintenance_mode() { 
    if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) { 
        wp_die('<h1>只今メンテナンス中です</h1><p>申し訳ありませんが、暫くしてから再度アクセスをお願いします。</p>'); 
    } 
} 
add_action( 'get_header', 'quick_maintenance_mode' );

ログインすると普通にそのままサイトを見ることができます。表示したい文言はwp_die()のカッコ内の記述を変えればOK。ちなみにこのままだと、タイトルは「サイト名 > エラー」と表示されます。タイトルを任意に変えたい場合は、以下のように記述します。

function quick_maintenance_mode() { 
    if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) { 
        wp_die('<h1>只今メンテナンス中です</h1><p>申し訳...</p>', 'メンテナンス中 | website name'); 
    } 
} 
add_action( 'get_header', 'quick_maintenance_mode' );

以上が、簡易的にメンテナンス画面を表示する場合。
このwp_dieをカスタマイズして、しっかりデザインしたメンテナンス画面を表示することもできます。

簡易的なメンテナンス画面ではなく、wp_dieをカスタマイズするにはfunctions.phpに関数を2つ用意します。
wp_dieで画面をレンダリングする関数(2)と、その関数名を指定する関数(1)です。

まずは(1)から、

function my_wp_die_handler($function){ 
    if(function_exists("get_bloginfo") && "" != get_bloginfo("template_directory")){ 
        return "my_wp_die"; 
    }else{ 
        return $function; 
    } 
} 
add_filter("wp_die_handler", "my_wp_die_handler", 1000);

wp_dieがWordPressがテーマを取得する前に実行されるかもしれないので、テーマディレクトリを参照できるかどうかを確認して、確認できたら後でつくる「my_wp_die」という関数を使い、確認できなかったら通常のものを使うというふうにしてあります。とはいえ、functions.phpに書いているので念のためといった感じです。

次は(2)の実際に画面をレンダリングする関数です。
wp-includes/functions.phpに定義されている関数「_default_wp_die_handler」の関数名を「my_wp_die」に変えて同じくfunctions.phpに記述します。
以下は、「_default_wp_die_handler」の関数名を「my_wp_die」にして中身はそのままコピーしたものです。

function my_wp_die( $message, $title = '', $args = array() ) {
	$defaults = array( 'response' => 500 );
	$r = wp_parse_args($args, $defaults);

	$have_gettext = function_exists('__');

	if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) {
		if ( empty( $title ) ) {
			$error_data = $message->get_error_data();
			if ( is_array( $error_data ) && isset( $error_data['title'] ) )
				$title = $error_data['title'];
		}
		$errors = $message->get_error_messages();
		switch ( count( $errors ) ) :
		case 0 :
			$message = '';
			break;
		case 1 :
			$message = "<p>{$errors[0]}</p>";
			break;
		default :
			$message = "<ul>\n\t\t<li>" . join( "</li>\n\t\t<li>", $errors ) . "</li>\n\t</ul>";
			break;
		endswitch;
	} elseif ( is_string( $message ) ) {
		$message = "<p>$message</p>";
	}

	if ( isset( $r['back_link'] ) && $r['back_link'] ) {
		$back_text = $have_gettext? __('&laquo; Back') : '&laquo; Back';
		$message .= "\n<p><a href='javascript:history.back()'>$back_text</a></p>";
	}

	if ( defined( 'WP_SITEURL' ) && '' != WP_SITEURL )
		$admin_dir = WP_SITEURL . '/wp-admin/';
	elseif ( function_exists( 'get_bloginfo' ) && '' != get_bloginfo( 'wpurl' ) )
		$admin_dir = get_bloginfo( 'wpurl' ) . '/wp-admin/';
	elseif ( strpos( $_SERVER['PHP_SELF'], 'wp-admin' ) !== false )
		$admin_dir = '';
	else
		$admin_dir = 'wp-admin/';

	if ( !function_exists( 'did_action' ) || !did_action( 'admin_head' ) ) :
	if ( !headers_sent() ) {
		status_header( $r['response'] );
		nocache_headers();
		header( 'Content-Type: text/html; charset=utf-8' );
	}

	if ( empty($title) )
		$title = $have_gettext ? __('WordPress &rsaquo; Error') : 'WordPress &rsaquo; Error';

	$text_direction = 'ltr';
	if ( isset($r['text_direction']) && 'rtl' == $r['text_direction'] )
		$text_direction = 'rtl';
	elseif ( function_exists( 'is_rtl' ) && is_rtl() )
		$text_direction = 'rtl';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php if ( function_exists( 'language_attributes' ) && function_exists( 'is_rtl' ) ) language_attributes(); else echo "dir='$text_direction'"; ?>>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title><?php echo $title ?></title>
	<link rel="stylesheet" href="<?php echo $admin_dir; ?>css/install.css" type="text/css" />
<?php
if ( 'rtl' == $text_direction ) : ?>
	<link rel="stylesheet" href="<?php echo $admin_dir; ?>css/install-rtl.css" type="text/css" />
<?php endif; ?>
</head>
<body id="error-page">
<?php endif; ?>
	<?php echo $message; ?>
</body>
</html>
<?php
	die();
}

59行目から74行目がメンテナンス画面の見た目の部分になります。
ここをカスタマイズすればOK。
例えば、head内で

<link rel="stylesheet" href="<?php bloginfo("stylesheet_url"); ?>" type="text/css" />

と書けば、テーマ内のstyle.cssが使えるのでいろいろ出来ますね。