我的本地主机上没有任何问题,但当我在服务器上测试我的代码时,每个页面的末尾都会显示这个通知。
我的代码:
<?php
ob_start();
include 'view.php';
$data = ob_get_contents();
ob_end_clean();
include 'master.php';
ob_end_flush(); // Problem is this line
我的本地主机上没有任何问题,但当我在服务器上测试我的代码时,每个页面的末尾都会显示这个通知。
我的代码:
<?php
ob_start();
include 'view.php';
$data = ob_get_contents();
ob_end_clean();
include 'master.php';
ob_end_flush(); // Problem is this line
我不建议完全禁用wp_ob_end_flush_all()
函数,也绝对不会关闭php.ini
文件中的zlib.output_compression
。以下是更好的方法,替换导致问题的源代码,并保留底层功能:
/**
* Proper ob_end_flush() for all levels
*
* This replaces the WordPress `wp_ob_end_flush_all()` function
* with a replacement that doesn't cause PHP notices.
*/
remove_action( 'shutdown', 'wp_ob_end_flush_all', 1 );
add_action( 'shutdown', function() {
while ( @ob_end_flush() );
} );
更多关于原因和为什么这可能是最好的方法的详细信息可以在此处找到:WordPress ob_end_flush()错误的快速修复
WordPress试图在关闭时刷新输出缓冲区,但由于您已经调用了ob_end_flush()
,因此失败了。
您应该能够保持压缩状态,只需取消挂钩刷新操作即可:
remove_action( 'shutdown', 'wp_ob_end_flush_all', 1 );
你现在可以手动调用ob_end_flush()
,并保持zlib压缩。
ob_get_level()
。PHP本身会在关闭时自动刷新所有现有的缓冲区,因此不再需要wp_ob_end_flush_all()
。它曾经是过去PHP 5.2兼容性问题所需的,对大多数服务器来说都很好,因此没有被删除。当WordPress醒来并删除该函数时最好,但目前删除关闭操作也是可以的,并且非常安全。 - Brian C在 php.ini
中关闭 zlib.output_compression
后问题得以解决。
zlib.output_compression = Off
出于安全考虑,您应该始终在实时网站上禁用前端错误 - 无论如何。
如果您想隐藏WordPress中的错误并获取错误日志以供检查,可以在wp-config.php文件中执行以下操作:
Original Answer翻译成"最初的回答"
// Enable WP_DEBUG mode
define( 'WP_DEBUG', true );
// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );
// Disable display of errors and warnings
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
PS: 如果您想使用alexg提供的remove_action代码,remove_action('shutdown', 'wp_ob_end_flush_all', 1);
您需要将其放置在主题的functions.php文件中。
PPS: 您还可以尝试在wp-config.php文件中使用define(‘WP_MEMORY_LIMIT’,’1024M’);
- 但是,请注意不要分配过多,因为这会影响WordPress前端,如果页面同时访问太多,您将面临内存不足的风险。
最初的回答:
如果您想使用alexg提供的remove_action代码,您需要将其放置在主题的functions.php文件中。另外,您还可以在wp-config.php文件中使用define('WP_MEMORY_LIMIT','1024M')来尝试解决问题,但请注意不要分配过多内存,以免影响WordPress前端并面临内存不足的风险。只需将以下内容添加到主题的functions.php文件中:
remove_action( 'shutdown', 'wp_ob_end_flush_all', 1 );
这个错误可能是由于没有关闭?>
标签引起的。我在遇到“插件在激活期间生成了X个意外输出字符”的错误后来到这里,然后启用了调试。我将index.php中的包含文件缩小到与/不与错误一起使用的目标文件。然后我进入那个文件,并使用PHP标签关闭了该文件,因为该文件中有许多函数。之后就可以正常工作了。
ob_get_clean();
只需在关闭挂钩上使用您的代码,并使位置更早 默认的ob_end_flush()将识别您的输出并将其刷新
add_action('shutdown', 'your_code', 0);
function your_code(){
/* Your Code Goes here */
}