OK, now that I installed WordPress and customized the Royal theme enough to suit my own megalomania and my girlfriend's taste, I try to post an empty test comment, expecting a nice error message, and... ouch! the digest authentication prompt guarding my administration directory?! why?

A quick grep reveals that the

wp_die()

function in

wp-includes/functions.php

uses its own hardcoded template, linking many assets under the protected

wp-admin

directory, e.g.

install.css

. Bad.

Looks like an "idea" about Modular error pages/a way to override wp_die exists in the Wordpress Ideas vault since two weeks ago. Good.
But I need them now, themed error pages. It's a matter of decency!

Luckily, it's quite easy, even if a wonderfully dirty hack...

  1. Create a new page (
    Write|Write Page

    ) called "Error", containing just the following code (be sure you write this in the "Code" tab):

    <!--ERROR_MESSAGE-->

    Double check that it's shown using the URL http://yourhost.com/path/to/your/blog/error

  2. Open the
    wp-includes/functions.php

    file in a text editor and locate the line

    if (strpos($_SERVER['PHP_SELF'], 'wp-admin') !== false)

    (should be around line 1340 in WP 2.2)

  3. Place the following code just before the line you've just located
    die(str_replace('<!--ERROR_MESSAGE-->', $message,
    file_get_contents(get_option('siteurl') . '/error')));

Done :)

Don't forget that you've just hacked a core WordPress file, so you'll have to repeat this operation on next upgrade... unless they add this feature, as we sincerely hope, or you mistyped something and just annihilated your blog. RIP.

12 Responses to “Themed Error Pages”

  1. #1 Aerik says:

    First you make Firefox safter for millions of people, and now you're hacking Wordpress.

    You're the man, Giorgio!

  2. #2 Andy says:

    Hi,
    Can't seem to get this to work. I am using WP 2.2
    I followed your instructions but when i type in mydomain.com/blog/error i get the following error
    Parse error: syntax error, unexpected '?' in /home/../public_html/blog/wp-content/themes/bello/page.php on line 16

    But i dont see anything in page.php that is erroneous.

    What do you think is happening? thanks

  3. #3 Giorgio says:

    Hi Andy,
    Could you please send me both your

    page.php

    and the

    wp-includes/functions.php

    you modified?
    Does this problem happen with other pages as well?

    This issue may depend on your url-rewriting options.
    If they're not set to display your new made "Error" page as

    , this trick won't work.
    You'll need to adjust the

    file_get_contents(get_option('siteurl') . '/error')

    statement to match the correct URL for your "Error" page.
    Good luck and let me know.

  4. #4 Andy says:

    Thanks Giorgio,

    So the problem seemed to be step one. Creating the page in wordpress. Not sure why?

    So I ended up creating my own error page in the theme editor, added it to my theme directory, then changed the path from '/error' to '/wp-content/themes/.../error.php' in the functions.php file.
    And it worked great!

    Not sure what i was doing wrong.

    I have another question. What does the first argument refer to in the wp_die function?
    in particular this part :

    die(str_replace('<--ERROR_MESSAGE-->', $message,

    As i don't have the page created as you recommend in step one what does the now refer to?

    cheers

  5. #5 Giorgio says:

    Andy, my

    die(str_replace('<--ERROR_MESSAGE-->', $message, file_get_contents(get_option('siteurl') . '/error')));

    statement works this way:

    1. file_get_contents(...)

      fetches the content of page located at

      get_option('siteurl') . '/error'

      or wherever you replaced '/error' with.

    2. str_replace(...)

      replaces the "

      <--ERROR_MESSAGE-->

      " string you should have put in the body of your custom page with the actual error message (

      $message

      )

    3. die(...)

      exits the scripts immediately after printing out the string prepared above.

  6. #6 Andy says:

    Thanks Giorgio,
    I get it!
    So i if i dont include the '<--ERROR_MESSAGE-->' in the body of my custom page ( which i haven't) then the string replace is just ignored and the message stored in $message is lost. I am sure this is bad practice though.

    So when else is the

    wp_die()

    function be called?

    cheers
    andy

  7. #7 Giorgio says:

    The

    wp_die()

    function is called by WordPress every time a fatal error happen.
    Unluckily, WordPress has a quite broad concept of "fatal", including stuff like comments omitting a mandatory field.
    It's not nice giving feedback about ordinary events like that on a naked white page.
    Hence the need for themed error pages.

  8. #8 andy says:

    Hi Giorgio,

    So a little off subject.
    I have created a popup window to deal with "add comments" on my blog. When the user doesn't include their name or write any comments, i have a custom error page that replaces the content in the popup with a custom message i have written. So the hack you provided really just allows this error page to be called.

    My concern is that if there are any other errors encountered this same error window will appear with my custom message, which will probably not relate to the error encountered. Now this can be easily solved by including $message in the body of the page like you recommend. But usually these error messages are pretty cryptic.
    Does wordpress have a list of error messages and under what circumstances they are likely to be encountered?

    Alternatively, how would you create a small window (like those error messages that the windows environment produces) that would popup to say "please enter name"
    Then i wouldn't need to customize an error page specifically for missing data.

    cheers
    Andy

  9. #9 Giorgio says:

    Hi Andy,

    I guess your problem is best handled using JavaScript client side validation.
    My hack intercepts any error message produced by WordPress, included the server side validation for comments.
    If you want your error messages to popup before the data is submitted, you can add a JavaScript

    onsubmit

    event handler to check the fields and

    alert()

    the feedback.
    Many themes (e.g. K2) already feature such a goodie out of the box.

    Hope it helps.

  10. #10 Andy says:

    Mate,
    I thank you! A lot!
    Andy

  11. #11 richard says:

    nice...very new to da whole net thing, me do mostly hardware, custom built. with os and the most common software that is currently used. just got ADSL and paying hefty price to but me try to cheat my sp cause they only allow 4gb...!!!what the @#$#!! 4gbis wat i do in 2 hours.....so i do google search for south african software brains. end no one to date. so me now no sleep and me try to get around this whole new world that is very very faccinating....i just cant close my eyes...and have little help with substance:) but substance make net for exciting...is me mad!!

  12. #12 Ugo says:

    Many Thanks. I have wp_die() function is called every time and i have an error happen.

Bad Behavior has blocked 2718 access attempts in the last 7 days.