1

I am adding the below code for the email notification when my wordpress site fas fatal error or other. But I am not getting the email when I rename the load.php file(i.e when fatal error occurs)

do_action( 'shutdown', $array );
function action_shutdown($array) {
$err = error_get_last();
if ( !$err) {
return;
}
$fatals = array(
E_USER_ERROR => 'Fatal Error',
E_ERROR => 'Fatal Error',
E_PARSE => 'Parse Error',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning'
);

if (isset($fatals[$err['type']])) {
$msg = $fatals[$err['type']] . ': ' . $err['message'] . ' in ';
$msg.= $err['file'] . ' on line ' . $err['line'];
$headers = "From: [email protected]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
error_log($msg, 1, "[email protected]", $headers);
}
}
add_action( 'shutdown', 'action_shutdown', 10, 1 );

I am adding this code in function.php file of theme. when error occurs on site it will not go inside the called function. but if running without error it is entering the called function. please tell me where i am wrong or using correct function.

3 Answers 3

2

I think you are misusing shutdown hook. WP attaches it to use with register_shutdown_function() and it's meant to process end of script execution, not log/process errors.

If you want to handle errors in a custom way you are better off using generic PHP set_error_handler() which is meant for that.

0

there are a couple errors/questions with your code.

  1. why are you using two do_actions( 'shutdown')?
  2. why does the action have an argument $array?
  3. where is the argument $array being set?
  4. why are the actions right next to your function on the functions.php file at that point you can simply add and call the function?

do_actions are hooks meant to give someone a way to hook into the WordPress event driven process in order to perform actions or alter current actions. meaning where ever you see a do_action() you can hook into and execute code. the arguments are meant to be able to do/alter something using those arguments. the arguments are passed by the do_action to the add_action callback function.

0

For anyone interested, I've released a plugin that does this, at https://wordpress.org/plugins/fatal-error-notify/. It hooks into PHP's register_shutdown_function() to handle the notifications. Check out /includes/class-public.php to see how it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.