Troubleshooting wordpress and wordpress plugins

Troubleshooting wordpress and its plugins can be a time consuming and a tedious process. WordPress plugins use hooks to add on new functionalities. The problem is that different plugins have different priorities. Worst still, some commercial plugins have their code obfuscated so its impossible to see what their code is doing. Fortunately, there are still some tricks that we can do to troubleshoot what’s going on.

1) Look at wp-includes/plugin.php. Most of the plugins will use add_action or add_filter. Just print out the variables in these functions so you know what hook it is calling and what function it is hooking on. eg

// in about line 82 of wp-includes/plugin.php
function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
  global $wp_filter, $merged_filters;

  // print_r to see the variables
  echo $tag.' '.$function_to_add;

  .....
}

2) Things become tricky if you the plugins that uses functions within a custom defined class. These classes have to be globally accessible before you can access the functions from other places. One good way to look for global variables is to use

print_r(array_keys($GLOBALS));

3) Say some plugin do this

class my_class {
  ...
  add_action('wp_login', array($this, 'xx_redirect'), 5, 2);

  function xx_redirect() {
    ..
  }
}

to remove the add_action from my_class, in your functions.php, you have to

global $my_class;
remove_action('wp_login', array($my_class, 'xx_redirect'), 5, 2);

Note that you have to specify the same no. of params and priority, ie 5 , 2.

4) Install xdebug and turn on error logging in your php.ini. Turn WP_DEBUG on in your wp-config.php

define('WP_DEBUG', false);

5) Use var_dump() and print_r() with exit to see your errors. Printing out $GLOBALS might crash your browser if you have lots of heavy plugins. Another way is to print them out in your log file.

error_log(print_r($var, 1));

6) Disable all plugins quickly can help troubleshooting

// backup your db and dump them somewhere, then quickly disable all plugins. after you've done your troubleshooting, re-enable them again
UPDATE wp_options SET option_value = 'a:0:{}' WHERE option_name = 'active_plugins';

7) View actual SQL

As most people are using wordpress functions to run queries, it is hard to see what the query actually is. One easy way to do it is to

// in config.php, add
define('SAVEQUERIES', true);

then in the part of the code that you want to see the query,

global $wpdb;
$posts_array = get_posts( $post_args );
// I don't know what get_posts is doing and like to see the sql for example, I will insert $wpdb->queries here
// note that you will be getting tonnes of saved queries.. but look at the last one. It will be the last query before exiting.
print_r($wpdb->queries);exit;

8) View your apache, php and mysql logs. Looking at these logs often gives you an idea of where went wrong.

These are my tips so far. Do you have any more to share?

cheers.

Author: bpeh

Bernard Peh is a great passioner of web technologies and one of the co-founder of Sitecritic.net Website Design and Reviews. He works with experienced web designers and developers everyday, developing and designing commercial websites. He specialises mainly in SEO and PHP programming.