Skip to content

Commit

Permalink
Log all WP_Error calls, when WP_ENV = 'development' (#557)
Browse files Browse the repository at this point in the history
Create errors.php
  • Loading branch information
EarthlingDavey authored Jun 24, 2024
1 parent d23e7c4 commit d05f80a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions public/app/mu-plugins/errors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Errors
* Only allowed in development environment.
*/

defined('ABSPATH') || exit;

if (getenv('WP_ENV') !== 'development') {
return;
}

/**
* wp_error_added
* When WP_Error is called, it is up to the developer to access and handle the error, they are not logged by default.
* This action/function ensures that any errors added to WP_Error will be logged immediately.
*/

add_action('wp_error_added', function (string|int $code, string $message, mixed $data, WP_Error $wp_error) {
if (is_array($message) || is_object($message)) {
error_log("Error code: $code. Message: " . print_r($message, true));
} else {
error_log("Error code: $code. Message: $message");
}
}, 10, 4);

0 comments on commit d05f80a

Please sign in to comment.