Last active
September 28, 2018 06:43
-
-
Save Tenderfeel/ddc974611a21217b635143ad100d8a9a to your computer and use it in GitHub Desktop.
味気ないwp_dieの出力を変更する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'>« 前のページに戻る</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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment