這個外掛會將以下偵錯常數加入 wp-config.php
檔案中,隨著外掛的啟用及停用,會加入及移除常數。任何錯誤都會觸發 PHP 例外狀況。請參考〈WordPress 的偵錯功能〉所列的偵錯常數。
預設設定:
define( 'WP_DEBUG_LOG', true );
define( 'SCRIPT_DEBUG', true );
define( 'SAVEQUERIES', true );
外掛啟用後,便會加入 @ini_set( 'display_errors', 1 );
。外掛首次執行時,便會將 WP_DEBUG
設定為 true
,之後可在外掛設定頁中變更這項設定。
使用者可在外掛設定頁中設定以下常數。
define( 'WP_DEBUG', true ); // Default on initial plugin installation.
define( 'WP_DEBUG_DISPLAY', false ); // Default when not declared is true.
define( 'WP_DISABLE_FATAL_ERROR_HANDLER', true ); // WordPress 5.2 WSOD Override.
外掛停用後,外掛會盡量重新加入預設的常數及常數值。外掛啟用後,會寫入預設設定並還原外掛停用前儲存的設定。
這個外掛使用 wp-cli/wp-config-transformer 命令,將常數寫入 wp-config.php
。
Andrew Norcross 開發的 Debug Quick Look 已整合至這個外掛中,協助閱讀 debug.log 檔案。如果網站已安裝 Debug Quick Look 外掛,請在啟用 WP Debugging 前刪除這個外掛。
Query Monitor 及 Debug Bar 為選用的相依性外掛,用來協助使用者進行偵錯及疑難排解。相依性外掛安裝通知訊息會在關閉訊息後 45 天再次顯示。
如果網站的 wp-config.php
儲存於非標準位置,請使用 wp_debugging_config_path
篩選器傳回網站安裝後的 wp-config.php
檔案位置。
wp_debugging_add_constants
篩選器能讓使用者將常數新增至 wp-config.php
中。
這個篩選器會傳回一個索引鍵為常數名稱的陣列,而常數值是一個資料陣列,其中包含字串值及布林值,以指示是否要在沒有引號的情況下傳遞這個值。
$my_constants = [
'my_test_constant' =>
[
'value' => 'abc123',
'raw' => false,
],
'another_test_constant' => [ 'value' => 'true' ],
];
value
選項包含作為常數值的字串。
raw
選項代表沒有引號的值,而不是將值視為字串置於設定中。預設值為 true
,如果是非布林值,請設定為 false
。
範例:
add_filter(
'wp_debugging_add_constants',
function( $added_constants ) {
$my_constants = [
'my_test_constant' => [
'value' => '124xyz',
'raw' => false,
],
'another_test_constant' => [ 'value' => 'true' ],
];
return array_merge( $added_constants, $my_constants );
},
10,
1
);
這個範例會建立以下常數。
define( 'MY_TEST_CONSTANT', '124xyz' );
define( 'ANOTHER_TEST_CONSTANT', true );
開發工作
歡迎在 GitHub 上對 develop 分支提出提取要求。