{"slug":"wordpress-debug-logging","title":"WordPress Debug Logging","tags":["debug","logging","wp-config","troubleshooting","query-monitor"],"agent_summary":"WordPress debug mode configuration in wp-config.php, reading debug.log via tail and grep, Query Monitor plugin for development, custom error logging to debug.log or custom log files, and common WordPress error patterns with causes.","trigger_phrases":["wordpress debug","wp_debug true","debug.log wordpress","wordpress error log","query monitor plugin","wordpress fatal error","wordpress error logging","wp-config debug settings"],"runnable":true,"markdown":"\nDebugging WordPress without logging is guessing. Enable debug logs for every development and troubleshooting session.\n\n## Enable Debug Mode (wp-config.php)\n\n```php\ndefine('WP_DEBUG', true);           // Enable debug mode\ndefine('WP_DEBUG_LOG', true);       // Write to wp-content/debug.log\ndefine('WP_DEBUG_DISPLAY', false);  // Don't display errors on screen (safe for production)\ndefine('SCRIPT_DEBUG', true);       // Load unminified scripts\ndefine('SAVEQUERIES', false);       // Set true to log DB queries (performance hit)\n```\n\n**Never set `WP_DEBUG_DISPLAY = true` on production.** It exposes code paths to visitors.\n\n## Read the Debug Log\n\n```bash\n# Watch in real time\ntail -f /var/www/html/wp-content/debug.log\n\n# Filter for errors only\ngrep -i \"fatal\\|error\\|warning\" /var/www/html/wp-content/debug.log | tail -50\n\n# Clear log (after resolving issues)\ntruncate -s 0 /var/www/html/wp-content/debug.log\n\n# Shared hosting (if SSH unavailable): use File Manager in cPanel\n```\n\n## Query Monitor Plugin (Development Only)\n\nInstall Query Monitor for detailed per-request diagnostics:\n- Database queries: count, slow queries, calling function\n- PHP errors and warnings (with file + line number)\n- Hook and filter execution order\n- HTTP API calls made during request\n- Conditional tags (is_single, is_archive, etc.)\n- Block editor: block types on page, render times\n\nDisable on production — adds overhead.\n\n## Custom Error Logging\n\n```php\n// Log to wp-content/debug.log\nerror_log('[MyPlugin] Lead saved: ' . $lead_id);\nerror_log('[MyPlugin] API response: ' . print_r($api_response, true));\n\n// Log to custom file with timestamp\nfunction my_log(string $message, string $context = 'general'): void {\n    $log_file = WP_CONTENT_DIR . '/my-plugin.log';\n    $line = '[' . date('Y-m-d H:i:s') . \"] [{$context}] {$message}\\n\";\n    error_log($line, 3, $log_file);\n}\n```\n\n## Common Error Patterns\n\n| Error Message | Likely Cause | Fix |\n|---------------|-------------|-----|\n| Fatal: allowed memory exhausted | Memory limit too low | Increase `WP_MEMORY_LIMIT` to 256M |\n| Fatal: Call to undefined function | Plugin not loaded or typo | Check plugin active, check function name |\n| Warning: Cannot modify header | Output before wp_head fires | Remove stray echo/whitespace |\n| PHP Parse error: syntax error | Typo in PHP code | Check line number in log, find missing `;` or `}` |\n| 500 error, empty debug.log | Apache/Nginx error | Check `/var/log/apache2/error.log` |\n| White screen of death | Fatal error with DISPLAY=false | Check debug.log immediately |\n\n## Server Error Log Locations\n\n```bash\n# Apache (Ubuntu/Debian)\n/var/log/apache2/error.log\n\n# Nginx\n/var/log/nginx/error.log\n\n# cPanel hosting\n~/logs/error_log\n\n# WPX Hosting: available via WPX control panel > Logs\n```\n","html":"<p>Debugging WordPress without logging is guessing. Enable debug logs for every development and troubleshooting session.</p>\n<h2>Enable Debug Mode (wp-config.php)</h2>\n<pre><code class=\"language-php\">define('WP_DEBUG', true);           // Enable debug mode\ndefine('WP_DEBUG_LOG', true);       // Write to wp-content/debug.log\ndefine('WP_DEBUG_DISPLAY', false);  // Don't display errors on screen (safe for production)\ndefine('SCRIPT_DEBUG', true);       // Load unminified scripts\ndefine('SAVEQUERIES', false);       // Set true to log DB queries (performance hit)\n</code></pre>\n<p><strong>Never set <code>WP_DEBUG_DISPLAY = true</code> on production.</strong> It exposes code paths to visitors.</p>\n<h2>Read the Debug Log</h2>\n<pre><code class=\"language-bash\"># Watch in real time\ntail -f /var/www/html/wp-content/debug.log\n\n# Filter for errors only\ngrep -i \"fatal\\|error\\|warning\" /var/www/html/wp-content/debug.log | tail -50\n\n# Clear log (after resolving issues)\ntruncate -s 0 /var/www/html/wp-content/debug.log\n\n# Shared hosting (if SSH unavailable): use File Manager in cPanel\n</code></pre>\n<h2>Query Monitor Plugin (Development Only)</h2>\n<p>Install Query Monitor for detailed per-request diagnostics:</p>\n<ul>\n<li>Database queries: count, slow queries, calling function</li>\n<li>PHP errors and warnings (with file + line number)</li>\n<li>Hook and filter execution order</li>\n<li>HTTP API calls made during request</li>\n<li>Conditional tags (is_single, is_archive, etc.)</li>\n<li>Block editor: block types on page, render times</li>\n</ul>\n<p>Disable on production — adds overhead.</p>\n<h2>Custom Error Logging</h2>\n<pre><code class=\"language-php\">// Log to wp-content/debug.log\nerror_log('[MyPlugin] Lead saved: ' . $lead_id);\nerror_log('[MyPlugin] API response: ' . print_r($api_response, true));\n\n// Log to custom file with timestamp\nfunction my_log(string $message, string $context = 'general'): void {\n    $log_file = WP_CONTENT_DIR . '/my-plugin.log';\n    $line = '[' . date('Y-m-d H:i:s') . \"] [{$context}] {$message}\\n\";\n    error_log($line, 3, $log_file);\n}\n</code></pre>\n<h2>Common Error Patterns</h2>\n<p>| Error Message | Likely Cause | Fix |\n|---------------|-------------|-----|\n| Fatal: allowed memory exhausted | Memory limit too low | Increase <code>WP_MEMORY_LIMIT</code> to 256M |\n| Fatal: Call to undefined function | Plugin not loaded or typo | Check plugin active, check function name |\n| Warning: Cannot modify header | Output before wp_head fires | Remove stray echo/whitespace |\n| PHP Parse error: syntax error | Typo in PHP code | Check line number in log, find missing <code>;</code> or <code>}</code> |\n| 500 error, empty debug.log | Apache/Nginx error | Check <code>/var/log/apache2/error.log</code> |\n| White screen of death | Fatal error with DISPLAY=false | Check debug.log immediately |</p>\n<h2>Server Error Log Locations</h2>\n<pre><code class=\"language-bash\"># Apache (Ubuntu/Debian)\n/var/log/apache2/error.log\n\n# Nginx\n/var/log/nginx/error.log\n\n# cPanel hosting\n~/logs/error_log\n\n# WPX Hosting: available via WPX control panel > Logs\n</code></pre>\n"}