Some handy WordPress debugging tips

If something goes wrong with your WordPress install due to a faulty plugin or some bad PHP code, it can often be tricky to work out exactly what the issue is an how to fix it. Here are a couple of helpful functions and snippets that you can use to fix a number of WordPress issues.

Turn debug mode on

This pretty much goes without saying – it’s the first thing you need to do as soon as something goes wrong and your WordPress site no longer loads any pages (in fact, you should always have it switched on when developing so you can be aware of PHP notices that could affect performance). When you switch debug mode on, all PHP errors, warnings and notices will be displayed in your browser. To switch it on simply open up wp-config.php in your site’s root folder, head down to line 81 and change the WP_DEBUG value to true:

define( 'WP_DEBUG', true );

This should be set back to false in a live environment.

Increase WordPress memory limit

Often a plugin will require more memory than your default server settings allow for – with debug mode on this will result in an error that reads along the lines of “Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 7680 bytes)”. To fix this you can assign WordPress more memory by adding this line to wp-config.php just below the WP_DEBUG line:

define( 'WP_MEMORY_LIMIT', '64M' );

Just set the memory limit value to whatever you need it to be – the 64 used in this case should be more than sufficient for most of your needs.

Prevent core script minification

Core WordPress scripts and styles are minified and concatenated by default in order to get the lowest page load speed possible. This makes debugging them incredibly difficult in the case of a Javascript conflict or something similar. You can switch off this minification by enabling script debugging – just add this line to your wp-config.php file just below the WP_DEBUG line:

define( 'SCRIPT_DEBUG', true );

While you can use this in a live environment, it is not recommended as it increases page load times quite a bit.

Store database query data

Every WordPress site makes loads of MySQL queries on each page load – most often these queries are dynamically generated and cannot easily be printed out. To remedy this you can save your queries along with backtrace data and execution time – simply add this line to your wp-config.php file just below the WP_DEBUG line:

define( 'SAVEQUERIES', true );

This will store your queries (along with their relevant data) inside the $wpdb->queries object for easy access. This setting is not to be used inside a live environment as it can increase page load time and will potentially give people access to your database structure and data.

Check if your code is actually running

If you’ve added an action and for some reason it’s just not doing what it’s supposed to be doing, it’s worth doing a quick check to see if the function is actually being called – simply add a die command to the top of the function like this:

die();

You’ll then find out very quickly if it’s working or not. This will help you work out if you’re using the wrong hook entirely or maybe just spelling the hook you want incorrectly.

Deactivate all plugins

If you can’t work out where the problem is coming in exactly, the best bet is to deactivate all your site’s plugins. Unless the problem is in the theme this will usually fix any issue temporarily. The process from there is to switch the plugins back on one-by-one until you find the culprit. There is a handy section on the plugins page that allows you to see a list of plugins that were recently active – this can be useful when dealing with a site that has many active and non-active plugins. To access this list just click the ‘Recently Active’ link at the top of your plugins page.

Switch to the default theme

If all else fails, switching back to the default WordPress theme will show you if there are any errors in your current theme. Matt Cohen wrote a handy plugin that adds a menu to your WordPress toolbar allowing you to quickly switch between themes without having to go to the Appearance > Themes page every time – you can get it here.

Use basic PHP functions

PHP has plenty of functions that are designed to help you debug your code more effectively – print_r(), var_dump() and die() being good examples. Get to know your PHP debugging functions as these will help you immeasurably in your development. As a side note, I find this little function is a handy replacement for print_r() as it shows the array in a more readable format:

function printr( $arr ) {
    echo "<pre>";
    print_r( $arr );
    echo "<pre>";
}

Google

Ultimately, if you can’t find a solution to your problem them simply Google it. WordPress has such an active community that you are almost guaranteed to find a solution to your problem from a quick search of the web.

If you have any other quick tips for debugging WordPress please post them in the comments.

Tags: , , , ,

No comments yet.

Leave a Reply