{"id":1498,"date":"2025-11-13T00:54:43","date_gmt":"2025-11-13T00:54:43","guid":{"rendered":"https:\/\/ykim.synology.me\/wordpress\/?p=1498"},"modified":"2025-11-13T03:54:47","modified_gmt":"2025-11-13T03:54:47","slug":"how-to-debug-php-code-in-wordpress","status":"publish","type":"post","link":"https:\/\/ykim.synology.me\/wordpress\/how-to-debug-php-code-in-wordpress-1498\/","title":{"rendered":"How to debug PHP code in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Debugging PHP code in WordPress involves a systematic approach using built-in WordPress tools, developer plugins, and specialized IDE (Integrated Development Environment) tools like <strong>Xdebug<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a guide covering the most effective methods, ranging from simple error logging to advanced step debugging.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u2699\ufe0f Basic Error Logging with <code>WP_DEBUG<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The most fundamental step is enabling WordPress&#8217;s built-in debugging constants, located in your site&#8217;s <code>wp-config.php<\/code> file. This is crucial for viewing PHP errors, warnings, and notices.<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Access <span style=\"color:OrangeRed\"><code>wp-config.php<\/code><\/span>:<\/strong> Connect to your site via <strong>SFTP\/FTP<\/strong> or use your hosting provider&#8217;s file manager. The file is usually in the root directory of your WordPress installation.<\/li>\n\n\n\n<li><strong>Enable Debugging:<\/strong> Find the line that says <code>\/* That's all, stop editing! Happy publishing. *\/<\/code> and insert the following constants <strong>before<\/strong> it:<br>PHP<br><span style=\"color:Blue\"><code>define( 'WP_DEBUG', true ); \/\/ Turns on the main debugging feature <\/code><br><code>define( 'WP_DEBUG_LOG', true ); \/\/ Forces all errors to be saved to a debug.log file <\/code><br><code>define( 'WP_DEBUG_DISPLAY', false ); \/\/ Prevents errors from displaying on the front-end (for production\/staging) <\/code><br><code>@ini_set( 'display_errors', 0 ); \/\/ Ensures PHP display errors are off<\/code><\/span><\/li>\n\n\n\n<li><strong>Trigger the Error:<\/strong> Reproduce the issue on your site (e.g., refresh the page where the bug occurs).<\/li>\n\n\n\n<li><strong>Check the Log:<\/strong> WordPress will create a file named <span style=\"color:OrangeRed; font-weight:bold\"><code>debug.log<\/code><\/span> inside the <span style=\"color:OrangeRed; font-weight:bold\"><code>\/wp-content\/<\/code><\/span> directory. Check this file for a <strong>call stack<\/strong> showing the file, line number, and function where the error occurred.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">2. \ud83d\udd0c Plugin-Based Debugging<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These plugins simplify the process by providing easy-to-read reports within the WordPress admin area.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">A. Debug Bar<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Install and activate the <strong>Debug Bar<\/strong> plugin.<\/li>\n\n\n\n<li>It adds a <strong>&#8220;Debug&#8221;<\/strong> menu item to the top WordPress admin bar, visible only to administrators.<\/li>\n\n\n\n<li>This bar provides quick access to query data, cache usage, PHP notices, and warnings.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">B. Query Monitor<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Install and activate the <strong>Query Monitor<\/strong> plugin.<\/li>\n\n\n\n<li>This is the most powerful tool for tracking performance issues, slow database queries, HTTP API calls, and displaying detailed information about PHP errors and template hierarchy directly in the admin bar. It is essential for tracking down slow code.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. \ud83d\udda5\ufe0f Advanced Step Debugging with Xdebug<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For fixing complex bugs, especially those involving variables changing across multiple functions, <strong>Xdebug<\/strong> is the gold standard. It allows you to freeze code execution and inspect every variable at any line.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">A. Setup Xdebug<\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Install Xdebug:<\/strong> Xdebug must be installed and configured on your PHP environment (local machine, staging server, or sometimes a managed host allows it).<\/li>\n\n\n\n<li><strong>Configure IDE:<\/strong> You need an IDE like <strong>VS Code<\/strong> (with the PHP Debug extension) or <strong>PhpStorm<\/strong> to listen for the Xdebug connection.<\/li>\n\n\n\n<li><strong>Start Listener:<\/strong> Activate the Xdebug listener in your IDE.<\/li>\n\n\n\n<li><strong>Browser Extension:<\/strong> Install a browser extension (like &#8220;Xdebug Helper&#8221;) to easily toggle the debugger on for your specific site.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">B. Debugging Process<\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Set Breakpoint:<\/strong> In your IDE, click on the line number in the code file you want to inspect (this sets a &#8220;breakpoint&#8221;).<\/li>\n\n\n\n<li><strong>Trigger Execution:<\/strong> With the listener active, visit the page on your WordPress site that triggers the code.<\/li>\n\n\n\n<li><strong>Inspect Variables:<\/strong> The IDE will &#8220;catch&#8221; the execution at the breakpoint. You can now view all variables, step <strong>Over<\/strong> the current line, step <strong>Into<\/strong> a function, or step <strong>Out<\/strong> of a function to trace the flow of execution.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">4. \ud83d\udda8\ufe0f Traditional <code>var_dump()<\/code> \/ <code>error_log()<\/code><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When you can&#8217;t use advanced tools, you can always rely on old-school functions to print variable contents.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>For simple output to the browser (use sparingly):<\/strong><br>PHP\n<span style=\"color:Blue\"><code>\/\/ Stop execution and print variable details <\/code><br><code>die( var_dump( $variable_name ) );<\/code><\/span><\/li>\n\n\n\n<li><strong>For logging to <code>debug.log<\/code> (recommended):<\/strong><br>PHP\n<span style=\"color:Blue\"><code>\/\/ Sends output to the \/wp-content\/debug.log file (if WP_DEBUG_LOG is true) <\/code><br><code>error_log( 'My variable value: ' . print_r( $variable_name, true ) );<\/code><\/span><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">How to suppress PHP Notices in debug.log<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n... TODO\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<div style='text-align:center' class='yasr-auto-insert-overall'><\/div><div style='text-align:center' class='yasr-auto-insert-visitor'><\/div>","protected":false},"excerpt":{"rendered":"<p>Debugging PHP code in WordPress involves a systematic approach using built-in WordPress tools, developer plugins, and specialized IDE (Integrated Development Environment) tools like Xdebug. Here is a guide covering the most effective methods, ranging from simple error logging to advanced step debugging. 1. \u2699\ufe0f Basic Error Logging with WP_DEBUG The most fundamental step is enabling&#8230;<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_kadence_starter_templates_imported_post":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","yasr_overall_rating":0,"yasr_post_is_review":"","yasr_auto_insert_disabled":"","yasr_review_type":"","fifu_image_url":"","fifu_image_alt":"","iawp_total_views":0,"footnotes":""},"categories":[87],"tags":[],"class_list":["post-1498","post","type-post","status-publish","format-standard","hentry","category-wordpress-slug"],"yasr_visitor_votes":{"stars_attributes":{"read_only":false,"span_bottom":false},"number_of_votes":0,"sum_votes":0},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts\/1498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/comments?post=1498"}],"version-history":[{"count":22,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts\/1498\/revisions"}],"predecessor-version":[{"id":1528,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts\/1498\/revisions\/1528"}],"wp:attachment":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/media?parent=1498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/categories?post=1498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/tags?post=1498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}