Disable PHP 7 Deprecated Notices from WordPress Plugins

Originally published at: https://roots.io/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…

4 Likes

Didn’t have any luck with this and could use some help.

Here’s part of my Bedrock composer.json file

 "require": {
    "php": ">=5.5",
    "composer/installers": "~1.0.12",
    "vlucas/phpdotenv": "^2.0.1",
    "johnpbloch/wordpress": "4.4.2",
    "oscarotero/env": "^1.0",
    "roots/wp-password-bcrypt": "1.0.0",
    "cweagans/composer-patches": "~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"
      }
    }
  }

When I run composer install I get

Gathering patches for root package.
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)
   Could not apply patch! Skipping.

Generating autoload files

Same error for me:

Could not apply patch! Skipping.

Did a composer update before the composer install too, otherwise it just refers to the composer.lock file and doesn’t install anything new.

Just ran into the same issue. Tried with the latest composer-patches version 1.4 and latest composer version
1.0-dev (134ce134a2a0c8c6b2c3b0ece2192a535875d42b) 2016-04-02 13:25:21

@cclass @discopatrick @vdrnn seems like a line ending issue with the patch file you’re trying. Give this one a shot:

https://dl.dropboxusercontent.com/u/145404/wordpress.patch

Another approach would be to use a custom error handler to ignore notices from specific plugins. That’s how Ostrichcize works.

A much simpler fix is to add this to the Custom Settings section of site/config/application.php

define('WP_DEBUG_DISPLAY', false);

But then you won’t see error messages on screen when you should be able to :slight_smile:

This blog post is hopefully irrelevant by now as PHP 7 adoption has increased.

Hi, I’m trying the same thing and it’s giving me the same error.

I even tried modifying /site/config/application.php and add
define(‘WP_DEBUG_DISPLAY’, false);

but then It gives 502 nginx error.

Isn’t there any solution to this??

Is anyone still using this tip? I struggled with this for a while before I realised that the patch should now be against johnpbloch/wordpress-core, not johnpbloch/wordpress as above.

In full, the patches section in composer.json should be:

     "patches": {
       "johnpbloch/wordpress-core": {
         "Prevent E_DEPRECATED error messages when debug is enabled": "wordpress.patch"
       }
     }

Note also that the patch linked in the article is not surprisingly also out of date. I find this works for me, instead:

--- wp-includes/load.php        2018-02-06 16:42:13.000000000 +0000
+++ wp-includes/load.php        2018-03-10 16:28:50.883310743 +0000
@@ -318,7 +318,7 @@
        }
 
        if ( WP_DEBUG ) {
-               error_reporting( E_ALL );
+               error_reporting( E_ALL & ~E_DEPRECATED );
 
                if ( WP_DEBUG_DISPLAY )
                        ini_set( 'display_errors', 1 );
2 Likes

Sorry for dragging this ancient thread back to life, but it seems pretty relevant again with PHP 8 becoming standard (and all the plugin deprecation notices that go with it :roll_eyes: )

Managed to find the original guide on wayback machine (still good after 6yrs!):

Here’s a short version:

  1. Copy this patch into: ./site/wp-mute-plugin-notices.patch

    --- web/wp/wp-includes/load.php
    +++ web/wp/wp-includes/load.php
    @@ -457,7 +457,7 @@ function wp_debug_mode() {
      }
    
      if ( WP_DEBUG ) {
    -		error_reporting( E_ALL );
    +		error_reporting( E_ALL  & ~E_DEPRECATED );
    
        if ( WP_DEBUG_DISPLAY ) {
          ini_set( 'display_errors', 1 );
    
  2. Add patch to composer.yml:

    ./site/composer.yml
    ...
    "wordpress-install-dir": "web/wp",
    + "patches": {
    +  "roots/wordpress": {
    +    "Prevent E_DEPRECATED error messages when debug is enabled": "wp-mute-plugin-notices.patch"
    +   }
    }
    
  3. Add patch package: composer require "cweagans/composer-patches"

  4. composer update + celebrate the the lack of plugin deprecation noise! :tada:

Also, If there’s a better way to do this, I’d really like to know.

4 Likes

Update for WP 6, make sure you use the correct package name, stumped me for a bit!

ie. for no-content (now the default) use:

+    "patches": {
+      "roots/wordpress-no-content": {

2 Likes

@ben Just an FYI that after upgrading to PHP 8.1 I’m getting these deprecation notices from Soil 4.0.5:

WP 6.3.1 & 6.4.1 (possibly lower versions) have different line numbers for the patch than mentioned above, also added a comment in the original gist for future.

--- web/wp/wp-includes/load.php
+++ web/wp/wp-includes/load.php
@@ -569,7 +569,7 @@ function wp_debug_mode() {
	}

	if ( WP_DEBUG ) {
-		error_reporting( E_ALL );
+		error_reporting( E_ALL & ~E_DEPRECATED );

		if ( WP_DEBUG_DISPLAY ) {
			ini_set( 'display_errors', 1 );
2 Likes