diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index c524f9e22a12f..99147bf8338d2 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -767,7 +767,7 @@ function rest_handle_deprecated_argument( $function_name, $message, $version ) { * @param string|null $version The version of WordPress where the message was added. */ function rest_handle_doing_it_wrong( $function_name, $message, $version ) { - if ( ! WP_DEBUG || headers_sent() ) { + if ( ! WP_DEBUG ) { return; } @@ -781,7 +781,22 @@ function rest_handle_doing_it_wrong( $function_name, $message, $version ) { $string = sprintf( $string, $function_name, $message ); } - header( sprintf( 'X-WP-DoingItWrong: %s', $string ) ); + if ( ! headers_sent() ) { + header( sprintf( 'X-WP-DoingItWrong: %s', $string ) ); + } + + if ( WP_DEBUG_LOG ) { + $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); + $caller = ''; + // Find the first caller outside of WordPress core (plugin or theme). + foreach ( $backtrace as $frame ) { + if ( isset( $frame['file'] ) && str_contains( $frame['file'], WP_CONTENT_DIR ) ) { + $caller = ' in ' . $frame['file'] . ' on line ' . $frame['line']; + break; + } + } + error_log( 'PHP Notice: ' . wp_strip_all_tags( $string ) . $caller ); + } } /**