LoginSignup
0
0

More than 1 year has passed since last update.

[WordPress] wp-config.php で error_reporting() を設定しても反映されない

Posted at

WordPress が出力するエラーを制御しようとして wp-config.php に error_reporting() を追加しても、全然反映されないって事象に遭遇したことありませんか?
WordPress でのエラー制御は WP_DEBUG という定数で制御されるのですが、この値によって error_reporting() が実行されるので、wp-config.php に error_reporting() を設定しても無視されるんです。

  • WP_DEBUG == true の場合: error_reporting( E_ALL );
  • WP_DEBUG == false の場合: 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 );

これは wp_debug_mode() という関数の中で実装されています。
無視して、任意の設定にしたい場合は以下のようにしなさいとコメントがありますので、そのようにしましょう。

	/**
	 * Filters whether to allow the debug mode check to occur.
	 *
	 * This filter runs before it can be used by plugins. It is designed for
	 * non-web runtimes. Returning false causes the `WP_DEBUG` and related
	 * constants to not be checked and the default PHP values for errors
	 * will be used unless you take care to update them yourself.
	 *
	 * To use this filter you must define a `$wp_filter` global before
	 * WordPress loads, usually in `wp-config.php`.
	 *
	 * Example:
	 *
	 *     $GLOBALS['wp_filter'] = array(
	 *         'enable_wp_debug_mode_checks' => array(
	 *             10 => array(
	 *                 array(
	 *                     'accepted_args' => 0,
	 *                     'function'      => function() {
	 *                         return false;
	 *                     },
	 *                 ),
	 *             ),
	 *         ),
	 *     );

wp-config.php に追加するコードはこんな感じになります。

// 本番用: Warning は出力しない
@ini_set( 'log_errors', 'On' );
@ini_set( 'display_errors', 'Off' );
define( 'WP_DEBUG', false );
define( 'WP_DEBUG_LOG', false );
define( 'WP_DEBUG_DISPLAY', false );
error_reporting( E_CORE_ERROR | E_COMPILE_ERROR | E_ERROR | E_PARSE | E_USER_ERROR );
// enable_wp_debug_mode_checks フィルターフックには false を返す
$GLOBALS['wp_filter'] = [
    'enable_wp_debug_mode_checks' => [
        10 => [
            [
                'accepted_args' => 0,
                'function'      => function() { return false; },
            ],
        ],
    ],
];
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0