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”]


Now add

[codesyntax lang=”php”]


right inside the if that starts with:

[codesyntax lang=”php”]


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:

  • DrV

    Thanks so much, will try it with the plugin I’m developing right now 😀

  • David Anderson

    Thanks for the post. I tried this but still, all I get is the “Plugin could not be activated because it triggered a fatal error.” Could you please help?

    Thanks alot

    • wvega

      What version of WordPress are you using? I’ll test snippet in that version and see if I can help you.

      • David Anderson

        Thanks for your kind offer. I’m using 3.5.1. I found a solution for my problem. The trick is to add:
        error_reporting(E_ALL ^ E_NOTICE);
        to the index.php of the plugin file.

        All errors were displayed.

  • vitorargos

    This piece of code helped a lot! Thank you!

  • Guest
    • wvega

      Thank you. That’s definitely a cleaner solution!

  • WPcoder16873245

    If you get the error “Plugin could not be activated because it triggered a fatal error.” and no other output, check that you didn’t accidentally forget an “exit” or “die” in your plugin code.

    It happened to me, and Google brought me to this page, so I’m leaving this note here for others.