php output buffer not working after php 7.4 to php 8.1 upgrade

1.2k Views Asked by At

The below code is not printing anything in the browser. actually, It should show the header menu. if I remove ob_start(); and ob_end_clean() at least its printing menu without CSS.

// Turn on output buffering HTML

ob_start();

echo preg_replace( '/\n|\t/i', '', implode( '' , $wr_nitro_header_html ) );

WR_Nitro_Header_Builder::prop( 'html', ob_get_contents() );

ob_end_clean()

update: same code is working fine for php7.4 but php8.1 is not working

2

There are 2 best solutions below

0
Marsellus On

Your theme may not be compatible with 8.1, as there are several breaking changes introduced when migrating from 7.4 to 8.1. Try adding:

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

to your wp-config.php file and see if errors print to the DOM, or make the display false and check debug.log if you are concerned about printing errors.

Then, reach out to the theme company directly and asking if they are compatible with PHP 8.1.

1
Dev Błack On

There is a compatibility issue because the Output Buffering has been changed in PHP 8.1

In PHP 8.1. you have to use the new aliases instead of using the PHP 7.4 ob_* functions.

For example:

use function OutputControl\ob_start;
use function OutputControl\ob_end_flush;
use function OutputControl\ob_get_clean;

ob_start();

echo "Hello, world!";

ob_end_flush();

$output = ob_get_clean();

Alternatively, you can use the OutputControl namespace like that:

use OutputControl;

// Enable output buffering
ob_start();

echo "Hello, world!";

ob_end_flush();

$output = ob_get_clean();