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
function in
uses its own hardcoded template, linking many assets under the protected
directory, e.g.
. 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...
- 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
- 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)
- 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.
August 1st, 2007 at 6:44 pm
First you make Firefox safter for millions of people, and now you're hacking Wordpress.
You're the man, Giorgio!
August 7th, 2007 at 1:37 pm
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
August 7th, 2007 at 3:49 pm
Hi Andy,
Could you please send me both your
and the
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
statement to match the correct URL for your "Error" page.
Good luck and let me know.
August 7th, 2007 at 7:07 pm
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 :
As i don't have the page created as you recommend in step one what does the now refer to?
cheers
August 7th, 2007 at 7:21 pm
Andy, my
statement works this way:
fetches the content of page located at
or wherever you replaced '/error' with.
replaces the "
" string you should have put in the body of your custom page with the actual error message (
)
exits the scripts immediately after printing out the string prepared above.
August 10th, 2007 at 4:38 pm
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
function be called?
cheers
andy
August 10th, 2007 at 4:56 pm
The
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.
August 14th, 2007 at 1:20 pm
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
August 14th, 2007 at 6:31 pm
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
event handler to check the fields and
the feedback.
Many themes (e.g. K2) already feature such a goodie out of the box.
Hope it helps.
August 16th, 2007 at 12:41 pm
Mate,
I thank you! A lot!
Andy
July 7th, 2008 at 4:06 am
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!!
March 2nd, 2010 at 12:48 pm
Many Thanks. I have wp_die() function is called every time and i have an error happen.