WordPress でプラグイン開発をするときにデバッグする方法についてです。最初に書いたプラグインは、地道に echo とか print_r とかしてたので、よりまともな方法をまとめておきます。
第1回目は、WP_DEBUG オプションについてです。
テーマやプラグインを開発する際には、必ず有効にしておきたいオプションです。wp-config-sample.php をコピーして作成した wp-config.php には、下記のような行が含まれているはずです。
/** * For developers: WordPress debugging mode. * * Change this to true to enable the display of notices during development. * It is strongly recommended that plugin and theme developers use WP_DEBUG * in their development environments. */ define('WP_DEBUG', false);
デフォルトでは、false になっていますが、これを下記のように true にすることで、エラーメッセージが WordPress 上に表示されるようになります。
/** * For developers: WordPress debugging mode. * * Change this to true to enable the display of notices during development. * It is strongly recommended that plugin and theme developers use WP_DEBUG * in their development environments. */ define('WP_DEBUG', true);
このオプション値を参照して、デバッグ情報の制御を行っているコードは、WordPress のスクリプト内に結構あります。その中で、一番重要なのは load.php ファイルの wp_debug_mode 関数です。
/** * Sets PHP error handling and handles WordPress debug mode. * * Uses three constants: WP_DEBUG, WP_DEBUG_DISPLAY, and WP_DEBUG_LOG. All three can be * defined in wp-config.php. Example: <code> define( 'WP_DEBUG', true ); </code> * * WP_DEBUG_DISPLAY and WP_DEBUG_LOG perform no function unless WP_DEBUG is true. * WP_DEBUG defaults to false. * * When WP_DEBUG is true, all PHP notices are reported. WordPress will also display * notices, including one when a deprecated WordPress function, function argument, * or file is used. Deprecated code may be removed from a later version. * * It is strongly recommended that plugin and theme developers use WP_DEBUG in their * development environments. * * When WP_DEBUG_DISPLAY is true, WordPress will force errors to be displayed. * WP_DEBUG_DISPLAY defaults to true. Defining it as null prevents WordPress from * changing the global configuration setting. Defining WP_DEBUG_DISPLAY as false * will force errors to be hidden. * * When WP_DEBUG_LOG is true, errors will be logged to wp-content/debug.log. * WP_DEBUG_LOG defaults to false. * * @access private * @since 3.0.0 */ function wp_debug_mode() { if ( WP_DEBUG ) { // E_DEPRECATED is a core PHP constant in PHP 5.3. Don't define this yourself. // The two statements are equivalent, just one is for 5.3+ and for less than 5.3. if ( defined( 'E_DEPRECATED' ) ) error_reporting( E_ALL & ~E_DEPRECATED & ~E_STRICT ); else error_reporting( E_ALL ); if ( WP_DEBUG_DISPLAY ) ini_set( 'display_errors', 1 ); elseif ( null !== WP_DEBUG_DISPLAY ) ini_set( 'display_errors', 0 ); if ( WP_DEBUG_LOG ) { ini_set( 'log_errors', 1 ); ini_set( 'error_log', WP_CONTENT_DIR . '/debug.log' ); } } else { 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 オプションが有効な場合の処理について説明します。
1つ目の if 文は、エラー出力のレベルを PHP 5.3 以降と PHP 5.2 以前で同じにするためのコードです。PHP 5.3 で E_DEPRECATED 定数が追加されたので、それで判定しています。PHP 5.3 系では、E_DEPRECATED が E_ALL にデフォルトで含まれ、PHP 5.4 系では、E_DEPRECATED と E_STRICT が E_ALL にデフォルトで含まれるので、両方に対応するために E_STRICT と E_ALL を除外しています。
2つ目の if 文は、PHP の実行時設定である display_errors 設定を変更しています。WP_DEBUG_DISPLAY オプションはデフォルトで true なので、display_errors 設定が有効になります。エラー出力のレベルを上げてエラーログには出力したいが、WordPress 上にエラーメッセージを出力したくない場合は、wp-config.php で下記のように設定すると良いと思います。
/** * For developers: WordPress debugging mode. * * Change this to true to enable the display of notices during development. * It is strongly recommended that plugin and theme developers use WP_DEBUG * in their development environments. */ define('WP_DEBUG', true); define('WP_DEBUG_DISPLAY', false);
3つ目の if 文は、PHP の実行時設定である log_errors 設定と error_log 設定を変更しています。WP_DEBUG_LOG オプションはデフォルトで false なので、明示的に設定しない限り PHP の実行時設定は変更されません。有効にしたとしても、エラーログの出力先が WP_CONTENT_DIR 定数で示される wp-content ディレクトリに設定されているため、一般公開している環境だと誰でもエラーログが閲覧できるようになってしまいます。WP_DEBUG_LOG オプションを明示的に true にしなければ、php.ini で設定されている設定値が有効になるので、エラーログの出力先は php.ini で設定することをおすすめします。WP_DEBUG_LOG オプションを明示的に true にした方がいい例としては、WordPress と密接に関わる PHP で記述されたシステムが同じサーバー上で同時に動くときに、エラーログの出力を分離したいときぐらいでしょうか。
WP_DEBUG オプションを有効にすると、ここにまとめたような効果が現れます。(ただし、最初に書いたとおり、WP_DEBUG オプションを参照してデバッグ情報の制御をしているところはほかにもあります。)テーマやプラグインを開発するときには WP_DEBUG オプションを有効にすることをおすすめします。