| | 18 | |
| | 19 | Example plugin which emits errors before and during the output buffer callback: |
| | 20 | |
| | 21 | {{{#!php |
| | 22 | <?php |
| | 23 | /** |
| | 24 | * Plugin Name: Emit Errors Before and During Output Buffer Callback |
| | 25 | * Requires at least: 6.9-alpha |
| | 26 | */ |
| | 27 | |
| | 28 | namespace EmitErrorsBeforeAndDuringOutputBufferCallback; |
| | 29 | |
| | 30 | function emit_notices() { |
| | 31 | _doing_it_wrong( __FUNCTION__, '✅ You are doing it wrong at ' . current_action(), '0.0.0' ); |
| | 32 | wp_trigger_error( __FUNCTION__, '✅ Notice ' . current_action(), E_USER_NOTICE ); |
| | 33 | wp_trigger_error( __FUNCTION__, '✅ Deprecated ' . current_action(), E_USER_DEPRECATED ); |
| | 34 | wp_trigger_error( __FUNCTION__, '✅ Warning ' . current_action(), E_USER_WARNING ); |
| | 35 | } |
| | 36 | |
| | 37 | add_action( 'wp_before_include_template', __NAMESPACE__ . '\emit_notices' ); |
| | 38 | |
| | 39 | add_filter( |
| | 40 | 'wp_template_enhancement_output_buffer', |
| | 41 | static function ( $buffer ) { |
| | 42 | emit_notices(); |
| | 43 | return $buffer; |
| | 44 | } |
| | 45 | ); |
| | 46 | |
| | 47 | }}} |
| | 48 | |
| | 49 | With PHP 8.5 active, this results in the following errors being printed before the HTML document is served: |
| | 50 | |
| | 51 | * Deprecated: ob_end_flush(): Producing output from user output handler wp_finalize_template_enhancement_output_buffer is deprecated in /var/www/src/wp-includes/functions.php on line 5481 |
| | 52 | * Notice: Function EmitErrorsBeforeAndDuringOutputBufferCallback\emit_notices was called incorrectly. ✅ You are doing it wrong at wp_before_include_template Please see Debugging in WordPress for more information. (This message was added in version 0.0.0.) in /var/www/src/wp-includes/functions.php on line 6131 |
| | 53 | * Notice: EmitErrorsBeforeAndDuringOutputBufferCallback\emit_notices(): ✅ Notice wp_before_include_template in /var/www/src/wp-includes/functions.php on line 6131 |
| | 54 | * Deprecated: EmitErrorsBeforeAndDuringOutputBufferCallback\emit_notices(): ✅ Deprecated wp_before_include_template in /var/www/src/wp-includes/functions.php on line 6131 |
| | 55 | * Warning: EmitErrorsBeforeAndDuringOutputBufferCallback\emit_notices(): ✅ Warning wp_before_include_template in /var/www/src/wp-includes/functions.php on line 6131 |
| | 56 | |
| | 57 | Notice only the four errors printed during the `wp_before_include_template` action are printed. None of the errors during the `wp_template_enhancement_output_buffer` filter are printed. They all appear in the error log, however: |
| | 58 | |
| | 59 | {{{ |
| | 60 | [16-Oct-2025 19:23:00 UTC] PHP Notice: Function EmitErrorsBeforeAndDuringOutputBufferCallback\emit_notices was called <strong>incorrectly</strong>. ✅ You are doing it wrong at wp_before_include_template Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 0.0.0.) in /var/www/src/wp-includes/functions.php on line 6131 |
| | 61 | [16-Oct-2025 19:23:00 UTC] PHP Notice: EmitErrorsBeforeAndDuringOutputBufferCallback\emit_notices(): ✅ Notice wp_before_include_template in /var/www/src/wp-includes/functions.php on line 6131 |
| | 62 | [16-Oct-2025 19:23:00 UTC] PHP Deprecated: EmitErrorsBeforeAndDuringOutputBufferCallback\emit_notices(): ✅ Deprecated wp_before_include_template in /var/www/src/wp-includes/functions.php on line 6131 |
| | 63 | [16-Oct-2025 19:23:00 UTC] PHP Warning: EmitErrorsBeforeAndDuringOutputBufferCallback\emit_notices(): ✅ Warning wp_before_include_template in /var/www/src/wp-includes/functions.php on line 6131 |
| | 64 | [16-Oct-2025 19:23:00 UTC] PHP Notice: Function EmitErrorsBeforeAndDuringOutputBufferCallback\emit_notices was called <strong>incorrectly</strong>. ✅ You are doing it wrong at wp_template_enhancement_output_buffer Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 0.0.0.) in /var/www/src/wp-includes/functions.php on line 6131 |
| | 65 | [16-Oct-2025 19:23:00 UTC] PHP Notice: EmitErrorsBeforeAndDuringOutputBufferCallback\emit_notices(): ✅ Notice wp_template_enhancement_output_buffer in /var/www/src/wp-includes/functions.php on line 6131 |
| | 66 | [16-Oct-2025 19:23:00 UTC] PHP Deprecated: EmitErrorsBeforeAndDuringOutputBufferCallback\emit_notices(): ✅ Deprecated wp_template_enhancement_output_buffer in /var/www/src/wp-includes/functions.php on line 6131 |
| | 67 | [16-Oct-2025 19:23:00 UTC] PHP Warning: EmitErrorsBeforeAndDuringOutputBufferCallback\emit_notices(): ✅ Warning wp_template_enhancement_output_buffer in /var/www/src/wp-includes/functions.php on line 6131 |
| | 68 | [16-Oct-2025 19:23:00 UTC] PHP Deprecated: ob_end_flush(): Producing output from user output handler wp_finalize_template_enhancement_output_buffer is deprecated in /var/www/src/wp-includes/functions.php on line 5481 |
| | 69 | [16-Oct-2025 19:23:00 UTC] PHP Deprecated: ob_end_flush(): Producing output from user output handler {closure:od_buffer_output():54} is deprecated in /var/www/src/wp-includes/functions.php on line 548 |
| | 70 | }}} |
| | 71 | |
| | 72 | In PHP 8.4, the deprecation notice for `ob_end_flush()` does not appear, and neither to any of the errors emitted during the output buffer callback. |