How to debug plugin activation errors in WordPress

If you have ever developed a plugin for WordPress you may remember seeing one of those unhelpful messages saying your plugin generated N characters of unexpected output.

The problem with those messages, apart from not knowing what exactly is what your plugin generated, is that sometimes the errors are not easy to reproduce. You go to your Plugins section, deactivate the plugin in question, try to activate it again and now all you see is “Plugin activated”, no errors.

The last time that happened to me I decided I wanted to see the content of those errors every time they occurred, and I came up with a solution to achieve just that. Is not pretty, but it works. Basically, I stop WordPress execution when this errors occur and show the output in my screen:

Open wp-admin/plugins.php file and look for the part that looks like this (line 42 in WP 3.4 files):

[codesyntax lang=”php”]

[/codesyntax]

Now add

[codesyntax lang=”php”]

[/codesyntax]

right inside the if that starts with:

[codesyntax lang=”php”]

[/codesyntax]

I know, it’s ugly, but you will be doing it in your development WordPress, so only you would see those errors and that’s the point.

You will have to modify that file again everytime you upgrade your WordPress or you can use a better approach combining this idea with the idea proposed here.

Let me know how do you handle those “unexpected output” error messages.

Related questions in Stack Overflow: