Disable PHP 7 Deprecated Notices from WordPress Plugins

If you’re running PHP 7 then you’ve probably ran into issues with deprecated notices from various WordPress plugins.

Trellis was recently upgraded with PHP 7 support so unfortunately we’ve seen a lot of these notices. If it was your own code, you’d want to fix these deprecations. But since we’re dealing with plugins, that’s not an option.

Example:

( ! ) Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; CoAuthorsIterator has a deprecated constructor in /app/plugins/co-authors-plus/template-tags.php on line 66

If you’re using PHP 7 and Xdebug then you can’t easily disable these errors. A solution in the meantime is to update WordPress core’s wp_debug_mode function to suppress E_DEPRECATED error messages.

Since you never want to modify WordPress core directly, we can use cweagans/composer-patches to apply a patch file with Composer to WordPress core.

Add cweagans/composer-patches to your composer.json file and place wordpress.patch in the same directory as your composer.json file.

Example composer.json file that will apply the patch:

  "require": {
    "php": ">=5.5",
    "composer/installers": "~1.0.12",
    "vlucas/phpdotenv": "^2.0.1",
    "cweagans/composer-patches": "~1.0",
    "johnpbloch/wordpress": "4.4.2",
    "oscarotero/env": "^1.0"
  },
  "extra": {
    "installer-paths": {
      "web/app/mu-plugins/{$name}/": ["type:wordpress-muplugin"],
      "web/app/plugins/{$name}/": ["type:wordpress-plugin"],
      "web/app/themes/{$name}/": ["type:wordpress-theme"]
    },
    "wordpress-install-dir": "web/wp",
    "patches": {
      "johnpbloch/wordpress": {
        "Prevent E_DEPRECATED error messages when debug is enabled": "wordpress.patch"
      }
    }    
  }

After making these changes to your composer.json file, run composer install for the patch to be applied:

vagrant@example:/srv/www/example.com/current$ composer install
Removing package johnpbloch/wordpress so that it can be re-installed and re-patched.
  - Removing johnpbloch/wordpress (4.4.2)
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Gathering patches for root package.
Gathering patches for dependencies. This might take a minute.
  - Installing johnpbloch/wordpress (4.4.2)
    Loading from cache

  - Applying patches for johnpbloch/wordpress
    wordpress.patch (Prevent E_DEPRECATED error messages when debug is enabled)

Now you can go back to developing without the notices. Have you run into any plugins that caused this issue for you? It wouldn’t hurt to report the issue upstream or even submit a PR.

Thanks to Craig for contributing this workflow and the patch file!

Read the discussion on our Discourse

Get our latest updates & occasional tips on building better WordPress sites

Follow @rootswp on Twitter