有没有办法在不修改 WordPress 中的 config-ini.php 文件的情况下从插件或主题打开或关闭 WP_DEBUG?
您可以通过在 WordPress 根目录下的
WP_DEBUG
文件中编辑以下代码来激活或停用 WordPress 安装中的 wp-config.php
。
define('WP_DEBUG', false);
您可以根据需要将参数更改为 true 或 false。
请务必注意,
WP_DEBUG
是恒定的,因此您无法更改它。但是,同样重要的是,它只是 wp_debug_mode()
使用的一个标志,它在 WordPress Core 中执行,因此位于主题和插件代码之前。这个函数的作用只是用 ini_set()
和 error_reporting()
更改一些 PHP 指令。因此,虽然您无法覆盖 WP_DEBUG
,但您当然可以在主题或插件中执行 wp_debug_mode()
后更改相同的 PHP 指令。
以下脚本只是通过一些检查来模拟 wp_debug_mode()
的行为。
function set_debug_mode($my_wp_debug, $my_wp_debug_display = true, $file_name = false)
{
if ($my_wp_debug && !WP_DEBUG && ini_get('error_reporting') != E_ALL) {
// Turn on debug
error_reporting(E_ALL);
if (WP_DEBUG_DISPLAY || $my_wp_debug_display) {
ini_set('display_errors', 1);
} elseif (null !== WP_DEBUG_DISPLAY) {
ini_set('display_errors', 0);
}
// Se the log file path using the name you provided with $file_name parameter (written without file extension) or the default path
$log_path = $file_name ? WP_CONTENT_DIR . '/' . $file_name .'.log' : get_log_path();
if ($log_path) {
ini_set('log_errors', 1);
ini_set('error_log', $log_path);
} else {
ini_set('log_errors', 0);
ini_set('error_log', '');
}
} elseif (!$acf_wp_debug && WP_DEBUG && ini_get('error_reporting') == E_ALL) {
// Turn off debug
// At this point,you may want to delete your log file.
// If yes, uncomment the following three lines
// $log_path = get_log_path();
// if (file_exists($log_path)) {
// unlink($log_path);
// }
error_reporting(E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR);
ini_set('display_errors', 0);
ini_set('log_errors', 0);
ini_set('error_log', '');
}
}
function get_log_path()
{
if (in_array(strtolower((string) WP_DEBUG_LOG), array('true', '1'), true)) {
$log_path = WP_CONTENT_DIR . '/debug.log';
} elseif (is_string(WP_DEBUG_LOG)) {
$log_path = WP_DEBUG_LOG;
} else {
$log_path = false;
}
return $log_path;
}
您可以使用挂钩在主题或插件执行中的某个时刻调用
set_debug_mode()
。例如,使用 init
:
add_action('init', function() {
// Turn debug on
set_debug_mode(true);
// Turn debug off
//set_debug_mode(false);
});