[WordPress] 味気ないwp_die()の出力を俺色に染める

メンテナンスページではなくエラーとかで死ぬ時に使うwp_die()が味気ないから変えたいっていう要望に答える

wp_die() で出力されている部分は、wp-includes/functions.phpの2934行目らへんにある _default_wp_die_handler()
これをフィルタ 'wp_die_handler' で上書きすれば変更できる。

手っ取り早いのは、_default_wp_die_handlerをテーマのfunctions.phpにコピペして、中身を俺色に染めたら add_filter で呼び出す方法。

1
2
3
4
function custom_wp_die_handler( $message, $title = '', $args = array() ) {
// イカしたdie画面のソース
}
add_filter( 'wp_die_handler', 'custom_wp_die_handler' );

クラスにしたい場合は、まずafter_setup_themeでインスタンスを作成してadd_actionでinitに登録:

1
2
3
4
5
add_action( 'after_setup_theme', function() {
  $customdie new CustomDie();
 
  add_action('init', array($customdie, 'enable_custom_die'));
} );

そして呼び出されるClass内のメソッドで wp_die_handler を登録する。

1
2
3
4
5
6
7
8
9
public function enable_custom_die() {
  add_filter( 'wp_die_handler', array($this, 'die_handler') );
}
 
public function die_handler($function){
  if(!is_admin()) {
    return array($this, 'die');
  }
}

クラス化したサンプル:

<?php
/**
* 味気ないwp_dieの出力を変更する
*/
class CustomDie {
public function __cosntruct() {
}
public function enable_custom_die() {
add_filter( 'wp_die_handler', array($this, 'die_handler') );
}
public function die_handler($function){
if(!is_admin()) {
return array($this, 'die');
}
}
/**
* Get maintenance mode page css
*
* @since 1.0
* @return string
*/
private function get_stylesheet() {
$filename = apply_filters('maintenance_css_filename', '/css/maintenance.css');
if (!validate_file($filename)) {
$url = apply_filters('maintenance_css_url', get_stylesheet_directory() . '/' . $filename);
if (file_exists($url)) {
$content = file_get_contents($url);
// CSS内のパスを変更
$content = preg_replace('/(?:\.\.\/images)/im', get_theme_file_uri('/images'), $content);
return '<style type="text/css">' . $content . '</style>';
}
}
return '';
}
/**
* wp_dieのカスタムハンドラ
*
* @since 1.0
* @see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/functions.php#L2832
*
* @param string|WP_Error $message Error message or WP_Error object.
* @param string $title Optional. Error title. Default empty.
* @param string|array $args Optional. Arguments to control behavior. Default empty array.
*/
public function die( $message, $title = '', $args = array() ) {
$defaults = array( 'response' => 500 );
$r = wp_parse_args($args, $defaults);
$back_link = '';
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;
}
} elseif ( !empty($message) ) {
$message = "<p>$message</p>";
}
if ( isset( $r['back_link'] ) && $r['back_link'] ) {
$back_link = "\n<p><a href='javascript:history.back()' class='back-link'>&laquo; 前のページに戻る</a></p>";
}
if ( ! 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 = 'エラー – ' . get_bloginfo('name');
?>
<!DOCTYPE html>
<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" />
<meta name="viewport" content="width=device-width">
<?php echo $this->get_stylesheet() ?>
<?php
if ( function_exists( 'wp_no_robots' ) ) {
wp_no_robots();
}
?>
<title><?php echo $title ?></title>
</head>
<body class="error-page">
<header class="site-header" ref="header">
<h1 class="site-title"><span class="site-logo">
<?php bloginfo( 'name' ); ?>
</span>
</h1>
</header>
<?php endif; // ! did_action( 'admin_head' ) ?>
<div class="error-content">
<img src="<?php print get_template_directory_uri() . '/images/page-not-found.png' ?>" width="100%" class="not-found-img" alt="お探しのページが見つかりませんでした">
<div class="error-message">
<?php echo $message; ?>
<a href="<?php bloginfo('home') ?>" class="goto-home">トップページへ</a>
<?php echo $back_link ?>
</div>
</div>
</body>
</html>
<?php
die();
}
}

コメントを残す

This site uses Akismet to reduce spam. Learn how your comment data is processed.