@@ -947,6 +947,22 @@ function wp_finalize_template_enhancement_output_buffer( string $output, int $ph
947947
948948 $ filtered_output = $ output ;
949949
950+ // TODO: Also capture exceptions?
951+ $ error_log = array ();
952+ $ display_errors = ini_get ( 'display_errors ' );
953+ if ( $ display_errors ) {
954+ ini_set ( 'display_errors ' , 0 );
955+ set_error_handler (
956+ static function ( int $ level , string $ message , ?string $ file = null , ?int $ line = null , ?array $ context = null ) use ( &$ error_log ) {
957+ if ( error_reporting () & $ level ) {
958+ $ error_log [] = compact ( 'level ' , 'message ' , 'file ' , 'line ' , 'context ' );
959+ }
960+ return false ;
961+ },
962+ E_ALL
963+ );
964+ }
965+
950966 /**
951967 * Filters the template enhancement output buffer prior to sending to the client.
952968 *
@@ -962,5 +978,37 @@ function wp_finalize_template_enhancement_output_buffer( string $output, int $ph
962978 * @param string $filtered_output HTML template enhancement output buffer.
963979 * @param string $output Original HTML template output buffer.
964980 */
965- return (string ) apply_filters ( 'wp_template_enhancement_output_buffer ' , $ filtered_output , $ output );
981+ $ filtered_output = (string ) apply_filters ( 'wp_template_enhancement_output_buffer ' , $ filtered_output , $ output );
982+
983+ if ( $ display_errors ) {
984+ foreach ( $ error_log as $ error ) {
985+ switch ( $ error ['level ' ] ) {
986+ case E_USER_NOTICE :
987+ $ type = 'Notice ' ;
988+ break ;
989+ case E_USER_DEPRECATED :
990+ $ type = 'Deprecated ' ;
991+ break ;
992+ case E_USER_WARNING :
993+ $ type = 'Warning ' ;
994+ break ;
995+ default :
996+ $ type = 'Error ' ;
997+ }
998+ $ displayed_error = sprintf ( "<br /> \n<b>%s</b>: %s " , $ type , $ error ['message ' ] );
999+ if ( null !== $ error ['file ' ] ) {
1000+ $ displayed_error .= sprintf ( ' in <b>%s</b> ' , $ error ['file ' ] );
1001+ if ( null !== $ error ['line ' ] ) {
1002+ $ displayed_error .= sprintf ( ' on line <b>%d</b> ' , $ error ['line ' ] );
1003+ }
1004+ }
1005+ $ displayed_error .= '<br /> ' ;
1006+
1007+ $ filtered_output .= $ displayed_error ;
1008+ }
1009+ restore_error_handler ();
1010+ ini_set ( 'display_errors ' , 1 );
1011+ }
1012+
1013+ return $ filtered_output ;
9661014}
0 commit comments