How to debug WordPress code

Debugging a problem in WordPress can be complicated and challenging. Below are some simple steps to follow that should help you catch 75% of the problems.

  1. Check the error logs for any clues related to the problem you are seeing. No error logs? Make sure the WP DEBUG is turned on.
  2. Flush premalinks, update plugins and disabled plugins that are most likely to cause issues.
  3. Review the raw html code for the page, check the browser console for any clues
  4. Add print statements in the code and display the content of critical variables along the way. The best way to display complex variables is to use the <pre> format combined with php functions like print_r, var_dump or var_export. Each of these takes the second argument of true/false to allow you to echo or return the output
echo "<pre>";
var_export($some_variable);
echo "</pre>";