Remove Strict Warnings in PHP 5.4

This is a quick post today. I had been struggling for a while with strict warnings popping up all over my projects since I upgraded my Vagrant box to PHP 5.4. Your initial reaction might be to fix the strict warnings, and you would be right except when you can’t.

It seems that a lot of very popular plugins and even WordPress core throw strict errors. The temptation then is to turn off WP_Debug to avoid scrolling through a long list of errors that you can’t fix anyway.

Before you do that, let me introduce you to a better way. Simply drop the following code into a blank file in the mu-plugin folder (if you don’t already have one, go ahead and create it).

if ( WP_DEBUG ) {
error_reporting( E_ALL & ~E_STRICT );
}

It is important that this code be added to an MU plugin for the code to get called after WordPress is done defining the error management and before the plugins are loaded.

3 thoughts on “Remove Strict Warnings in PHP 5.4”

  1. I’m pretty strict about notices (er, no pun intended), so I figured out a way to leave E_STRICT enabled while still avoiding notices from poorly written plugins that you have to run for one reason or another. It has the added benefit of ignoring all notices from those plugins, rather than just E_STRICT ones.

    You can define an array of plugin/theme slugs that you want to ignore notices from, and then override PHP’s default error handler to check whether the error originated one of those plugins. If it did, the custom error handler ignores it, but if it didn’t then the handler treats it like a normal error.

    More details and code are at http://iandunn.name/hide-php-warnings-and-notices-from-poorly-written-plugins/

    1. Thanks for the comment Ian! I just recently also found this plugin that sounds like it does the same thing. Ultimately you are right, I hadn’t thought of disabling errors by plugin, but that is a much better way to go if possible as I would prefer to keep all errors out of my code. 🙂

Leave a Reply