Skip to main content

Errors, Exceptions, Logging, and Debugging

PHP Error Types

TypeMeaning
Parse errorInvalid PHP syntax
Fatal errorExecution cannot continue
WarningProblem occurred, execution may continue
NoticeUndefined variable/key or similar issue
DeprecatedAPI or behavior should be replaced

WordPress Debug Constants

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
define('SCRIPT_DEBUG', true);

Use on development or staging. Do not display errors publicly in production.

Logging

error_log('Reached checkout handler.');
error_log(print_r($settings, true));

Never log passwords, tokens, cookies, full authorization headers, or private user data.

Exceptions

try {
myplugin_risky_operation();
} catch (Throwable $e) {
error_log($e->getMessage());
}

Catch Throwable if you need to catch both Exception and Error.

WP_Error

Many WordPress APIs return WP_Error.

$response = wp_remote_get('https://api.example.com/items');

if (is_wp_error($response)) {
error_log($response->get_error_message());
return [];
}

Create a WP_Error for REST or internal failures.

return new WP_Error(
'myplugin_invalid_request',
__('Invalid request.', 'my-plugin'),
['status' => 400]
);

AJAX Error Response

wp_send_json_error([
'message' => __('Invalid request.', 'my-plugin'),
], 400);

Debugging Checklist

  • Reproduce the issue with the smallest action possible.
  • Check PHP error logs.
  • Confirm the hook or route actually runs.
  • Confirm current user and capability.
  • Confirm input after unslash and sanitization.
  • Check whether the failing API returned WP_Error.
  • Disable caching if the symptom is stale output.
  • Test with only required plugins if conflict is suspected.

Common Problems

SymptomCommon Cause
White screenFatal error
Headers already sentOutput before redirect/header
Callback not runningWrong hook or file not loaded
REST 404Route registered too late or permalinks need flush
AJAX returns 0Missing action or handler output issue
Form saves nothingNonce, capability, or name mismatch
Infinite redirectRedirect condition never stops matching

Production Incident Rules

  • Do not edit WordPress core.
  • Make one change at a time.
  • Preserve logs and backups.
  • Disable the suspected plugin or theme safely if needed.
  • Roll back code before experimenting with database changes.
  • After recovery, write down the cause and prevention.