Skip to content

Commit 7e27cc2

Browse files
committed
REST API: Log doing_it_wrong notices to debug.log
Fixes #64260. The doing_it_wrong_trigger_error filter was preventing all doing_it_wrong notices from being logged during REST requests. This change ensures notices are written to debug.log while still preventing trigger_error() from interfering with REST responses.
1 parent dc6ce7a commit 7e27cc2

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/wp-includes/rest-api.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,36 @@ function rest_api_default_filters() {
245245
add_action( 'deprecated_argument_run', 'rest_handle_deprecated_argument', 10, 3 );
246246
add_filter( 'deprecated_argument_trigger_error', '__return_false' );
247247
add_action( 'doing_it_wrong_run', 'rest_handle_doing_it_wrong', 10, 3 );
248-
add_filter( 'doing_it_wrong_trigger_error', '__return_false' );
248+
add_filter( 'doing_it_wrong_trigger_error', 'rest_handle_doing_it_wrong_trigger_error', 10, 4 );
249+
}
250+
251+
/**
252+
* Handles doing_it_wrong errors for REST API requests.
253+
*
254+
* Logs doing_it_wrong notices to the debug log during REST API requests
255+
* while preventing trigger_error() from interfering with JSON responses.
256+
*
257+
* @since 7.0.0
258+
*
259+
* @param bool $trigger Whether to trigger an error.
260+
* @param string $function_name The function that was called incorrectly.
261+
* @param string $message A message explaining what was called incorrectly.
262+
* @param string $version The version of WordPress where the message was added.
263+
* @return bool Always returns false to prevent trigger_error().
264+
*/
265+
function rest_handle_doing_it_wrong_trigger_error( $trigger, $function_name, $message, $version ) {
266+
if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
267+
$log_message = sprintf(
268+
'REST API - Doing it wrong: %s - %s',
269+
$function_name,
270+
$message
271+
);
272+
if ( $version ) {
273+
$log_message .= sprintf( ' (This message was added in version %s.)', $version );
274+
}
275+
error_log( $log_message );
276+
}
277+
return false;
249278
}
250279

251280
// Default serving.

0 commit comments

Comments
 (0)