Debug Bar Slow Actions

If a typical WordPress page load takes more than one second, chances are there’s something terribly wrong with your site, a theme or a plugin is probably doing something it shouldn’t. Obviously you don’t have time to review every single line in your codebase, so Debug Bar Slow Actions might help you out.

Debug Bar Slow Actions is an extension for the popular Debug Bar plugin. It adds a new panel with a list of the top 100 slowest actions (and filters) during the current page request.

Debug Bar Slow Actions

It’s fairly lightweight (as opposed to xdebug and other profiling tools), but I wouldn’t recommend running it on a production environment, or at least not for long. It does hook to each and every action and filter (twice!) to time it, though timing is pretty fast and it’s highly unlikely that it’ll ever become a performance bottleneck.

After you’ve found out which action is the slowest, you can easily lookup the callback functions hooked to that action, perhaps using tools like Query Monitor, or a simple var_dump:

add_action( 'init', 'whats_at_init', 9999 );
function whats_at_init() {
	var_dump( $GLOBALS['wp_filter']['init'] );

Update: version 0.8.1 and above will show you the number of callbacks hooked to each action, and it’s also possible to expand the callbacks list.

You can get Debug Bar Slow Actions from or GitHub.

Happy profiling!

Toggle Elements States in Chrome’s Developer Tools

Yeah, I have only discovered this trick today. It was a nightmare debugging :hover (and other) states, and all the magic was hidden behind that tiny little button. Glad I finally found it, and hope you did as well! Did you?

WordPress: Oh Those Actions and Filters!

Reading Joost de Valk’s quick post about Simple WordPress Debugging with a query variable made me think for a while. Really, how often do you come accross a white screen with no clue of what’s happening? Very effective indeed, and good note about the security issue, but anyways, what came into my mind is a life-saver for all WordPress themes and plugins developers.

I came accross this following snippet quite some time ago on IRC I believe, but never shared it for no specific reason. Perhaps I thought that everybody is aware of it, but looking at how more and more people struggle with debugging WordPress actions and filters, ugh. Here’s your life-saver:

add_action('all', create_function('', 'var_dump(current_filter());'));

This should be used in functions.php or some plugin, and at first sight you’ll notice a huge bunch of text on the output. Fire up the source view of the page you’re looking at (works for admin panels too) and look through the code. You’ll notice that all actions and filters are printed on screen whenever they’re fired.

How could that be used? It clearly shows the order of each action and filter and it clearly shows the names (which tend to be forgotten sometimes). Maybe there are a few more pros I can’t think of right now. Downsides? It doesn’t actually tell you whether it’s an action or a filter, but anyways – short, useful, could be added up to Joost’s snippet.

PHP: Logger and Timer Snippets

Hey. I’ve just started writing my simplest classes for a new content management micro-system in php, and I’d like to share some moments with you. These classes will probably be good for php noobs out there struggling in the world of object-oriented programming – go for the basics. The Logger class is just a text logger with simple functionality good for logics debugging. The Timer class will be great for performance debugging. Anyways, here we go:

class Logger
	public $filename;
	public $mode;
	public $timestamp;
	public $linebreak;

	public $contents;

	function __construct($filename = "log.log", $timestamp = "d.m.Y h:i:s", $linebreak = "rn", $mode = "a+")
		$this->filename = $filename;
		$this->mode = $mode;
		$this->timestamp = date($timestamp);
		$this->linebreak = $linebreak;
		$this->contents = "";

	function __destruct() { }

	function write($line)
		$this->contents .= "{$this->timestamp} {$line} {$this->linebreak}";

	function flush()
		$handle = fopen($this->filename, $this->mode);
		fwrite($handle, $this->contents);
		$this->contents = "";

class Timer
	public $name;
	public $start;
	public $stop;
	public $format;

	const FORMAT_S = 0;
	const FORMAT_MS = 1;

	function __construct($name = "Timer", $autostart = false, $format = self::FORMAT_S)
		$this->name = $name;
		$this->format = $format;
		if ($autostart) $this->start();

	function __destruct() { }

	function start()
		$this->start = microtime(true);

	function stop()
		$this->stop = microtime(true);
		$interval = $this->stop - $this->start;
		if ($this->format == self::FORMAT_MS) { $interval *= 1000; }
		return $interval;

And this is how you use it:

$log = new Logger();
$log->write("some message");

$timer = new Timer("Timer1", false, $this::FORMAT_MS);
echo "execution time: " . $timer->stop() . " ms";

I doubt that I need to explain this, but anyways, you’re always welcome to question in the comments. By the way, it seems that there’s a bug in php 5 that prevents us from using fwrite in class destructors, so I couldn’t flush on destruct.