最近、いくつかのバグがあるWordPressプラグインを継承しました。私の問題は、WordPressも初めてで、何が起こっているのかを理解できるようにデバッグメッセージをログに記録する方法がわからないことです。
ポップアップを作成したり、コンソールにログインしたりする方法が本当に必要です。
WordPress Stack Exchangeにはこの優れたQ&Aがあり、多くの知識のある人々がデバッグ手法を説明しています。プラグインをどのようにデバッグしますか?
Javascriptの分野では、基本的にが必要<script>console.log('the value is' + variable);</script>
です。また、GoogleChromeインスペクターやFirebugを使用してください。
PHPでは、それはどこで起こっているか、またはどこで出力したいかによって異なります。
コーデックスの公式ドキュメント。
wp-config.php
デバッグの例
// 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 );
// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define( 'SCRIPT_DEBUG', true );
以下は、OSX / Unix / Linuxシステムパスを使用し、Windows用に調整します。
/* Log to File
* Description: Log into system php error log, usefull for Ajax and stuff that FirePHP doesn't catch
*/
function my_log_file( $msg, $name = '' )
{
// Print the name of the calling function if $name is left empty
$trace=debug_backtrace();
$name = ( '' == $name ) ? $trace[1]['function'] : $name;
$error_dir = '/Applications/MAMP/logs/php_error.log';
$msg = print_r( $msg, true );
$log = $name . " | " . $msg . "\n";
error_log( $log, 3, $error_dir );
}
次に、コードで関数を呼び出しますmy_log_file( $post, 'The post contents are:' );
/* Echo variable
* Description: Uses <pre> and print_r to display a variable in formated fashion
*/
function echo_log( $what )
{
echo '<pre>'.print_r( $what, true ).'</pre>';
}
そして、必要な場所では、次のように使用しますecho_log( $post );
。
この拡張機能は、ブラウザコンソールに直接情報を記録します。WordPress Answersの次のQ&Aを参照してください:WP-FirePHP拡張機能の使用方法は?。
頑張ってください、そしてあなたは私たちを最新の状態に保つかもしれません。
一般的なPHPデバッグ戦略は、print_r( $var )
ステートメントを使用してページを更新することです。シンプルで簡単。コードにステップインしたい場合は、 Xdebugがインストールしたいものです。