Skip to content

Instantly share code, notes, and snippets.

@nat-c
Last active February 9, 2023 10:37

Revisions

  1. nat-c revised this gist Jun 8, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion nc-custom-wp-error-pages
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    <?php

    add_filter( 'wp_die_handler', 'nc_get_custom_error_handler' );

    function nc_get_custom_error_handler() {
    return 'nc_custom_error_handler';
    }

    function nc_custom_error_handler( $message, $title = '', $args = array() ) {
    $defaults = array( 'response' => 500, 'back_link' => false );
    $r = wp_parse_args($args, $defaults);
    @@ -56,7 +58,7 @@ function nc_custom_error_handler( $message, $title = '', $args = array() ) {
    ?>
    <!DOCTYPE html>
    <!-- You might need to add a few random characters in a comment if your html outputs a file less than 512 bytes (even if gzipped). : IE bug -->
    <?php if ( function_exists( 'language_attributes' ) && function_exists( 'is_rtl' ) ) language_attributes(); else echo "dir='$text_direction'"; ?>>
    <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>
  2. nat-c created this gist Mar 26, 2015.
    167 changes: 167 additions & 0 deletions nc-custom-wp-error-pages
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,167 @@
    <?php

    add_filter( 'wp_die_handler', 'nc_get_custom_error_handler' );
    function nc_get_custom_error_handler() {
    return 'nc_custom_error_handler';
    }
    function nc_custom_error_handler( $message, $title = '', $args = array() ) {
    $defaults = array( 'response' => 500, 'back_link' => false );
    $r = wp_parse_args($args, $defaults);

    $have_gettext = function_exists('__');

    if ( is_string( $message ) ) {
    $message = "<p>$message</p>";
    }
    elseif ( 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;
    }
    }
    else {
    $message = "<h1>Bummer!</h1><p>Something went wrong and we are trying to figure it out. Check back soon!</p>"; //add your own default message
    }
    if ( isset( $r['back_link'] ) && $r['back_link'] ) {
    $back_text = $have_gettext? __('&laquo; Go Back') : '&laquo; Go Back';
    $message .= "\n<p><a href='javascript:history.back()'>$back_text</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 = $have_gettext ? __('Huston we have a problem!') : 'Huston we have a problem'; //add your own default title

    $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>
    <!-- You might need to add a few random characters in a comment if your html outputs a file less than 512 bytes (even if gzipped). : IE bug -->
    <?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>
    <style type="text/css">
    /** You'll need to alter with your own styles of course **/
    html {
    background: #C8AEE8;
    }
    #error-page {
    font-family: Helvetica, Arial, sans-serif;
    color: #314E55;
    margin: 50px auto;
    padding: 5% auto;
    }
    .error-content {
    background: #fff;
    margin: auto;
    max-width: 630px;
    border-radius: 15px;
    -webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13);
    box-shadow: 0 1px 3px rgba(0,0,0,0.13);
    }
    .error-content *::selection {
    color: #fff;
    background: #73C9C9;
    }
    .error-content *::-moz-selection {
    color: #fff;
    background: #73C9C9;
    }
    .error-content p,
    .error-content ul {
    font-size: 1em;
    line-height: 1.5;
    padding: 0 26px 26px;
    }
    .error-content ul {
    margin-left: 26px;
    }
    .error-content ul li {
    margin-bottom: 10px;
    }
    .error-content ul li:last-child {
    margin-bottom: 0;
    }
    .error-content h1 {
    font-size: 1.8em;
    padding: 26px 26px 0;
    text-align: center;
    }
    a {
    color: #73C9C9;
    text-decoration: none;
    }
    a:hover {
    color: #314E55;
    }
    .button {
    background: #73C9C9;
    border: 1px solid #73C9C9;
    color: #fff;
    display: inline-block;
    text-decoration: none;
    cursor: pointer;
    -webkit-border-radius: 5px;
    -webkit-appearance: none;
    border-radius: 5px;
    white-space: nowrap;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    margin: 0;
    vertical-align: middle;
    line-height: normal;
    height: auto;
    margin-bottom: 4px;
    padding: 0 20px 2px;
    font-size: 1.02em;
    text-align: center;
    box-shadow: none;
    }
    .button.button-large {
    font-size: 1.35em;
    padding: 0px 35px 2px;
    }
    .button:hover,
    .button:focus {
    background: #95A9CF;
    border-color: #95A9CF;
    box-shadow: 0px 0px 2px rgba(49, 78, 85, 0.5);
    }
    .button:active {
    box-shadow: 0px 1px 0px rgba(0, 0, 0, 0.1) inset;
    }
    </style>
    </head>
    <!-- Change the layout and structure the body however you want -->
    <body id="error-page">
    <?php endif; // ! did_action( 'admin_head' )
    ?>
    <div class="error-content">
    <?php echo $message; ?>
    </div>
    </body>
    </html>
    <?php
    die();
    }