Tag Archives: debug

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);
		fclose($handle);
		$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");
$log->flush();

$timer = new Timer("Timer1", false, $this::FORMAT_MS);
$timer->start();
sleep(1);
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.