4

The problem:

I want to run Wordpress 6.x with PHP 8.x in development mode - meaning define('WP_DEBUG', true); but Wordpress 6.x partial support for PHP 8.x throws a lot of deprecated warnings which do a lot of mess on the screen and also mess with cookies and REST API.

Setting error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED); in php.ini or wp-config.php does not solve the problem because Wordpress overwrites this setting when you set define('WP_DEBUG', true); which you want normally have for the development.

The solution is no straight forward and I couldn't find fast, either on Stack or anywhere else so below I will answer my own question and solve the problem for others.

2
  • error logging should be done to a file rather than printing to the screen, but keep in mind that support for 8.0 is incomplete, I'd advise avoiding 8.0 for now if these are a concern, especially when using plugins and purchased themes
    – Tom J Nowell
    Commented Jun 7, 2022 at 9:32
  • For development personally I like to see errors on the screen. Regarding the 8.0 readiness - yeah that is my concern - normally I use only narrow set of plugins (which are frequently updated) so at the moment I'll try and see what happens. The thing is that PHP is recently really quick with deprecating it's PHP versions 7.4 is supported only till the end of 2022 so there's really not much time and given the lifetime of an average template I thing one should really try the 8.x as soon as possible.
    – Picard
    Commented Jun 7, 2022 at 10:23

3 Answers 3

9

change the line that sets error_reporting in wp-includes/load.php, probably around line 450:

if ( WP_DEBUG ) {
    error_reporting( E_ALL);

just add the & ~E_DEPRECATED bit so it becomes:

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

[wordpress rant] - why would they not put something like this in the config file? In my (painful) experience of writing plugins & child themes for wordpress it's impossible to use debug mode not because of problems in my code but deprecated code in literally dozens of files throughout the codebase, ALWAYS. This renders WP_DEBUG useless. And don't get me started about how simple it would be to implement by default the multi-environment support I always have to hack into the wp-config.php file (I assume everyone else probably does this too) :-( [rant over(!!)]

2
  • 1
    This rocks. Thanks
    – Philip
    Commented Nov 18, 2022 at 9:35
  • 1
    Thanks for this. It's unbelievably stupid that WP doesn't respect the settings set in the php config file, that it overwrites the error reporting settings with its own idea of what error reporting should be, that this isn't configurable, and finally, that it requires editing the core files to fix.
    – lucian303
    Commented Jan 11, 2023 at 18:23
7

The key to the solution is enable_wp_debug_mode_checks filter but to use it you have to do something special.

As the documentation says:

This filter runs before it can be used by plugins. It is designed for non-web runtimes. Returning false causes the WP_DEBUG and related constants to not be checked and the default PHP values for errors will be used unless you take care to update them yourself.

To use this filter you must define a $wp_filter global before WordPress loads, usually in wp-config.php.

Then when you disable this default error reporting overwrite - you can set your own. So the final solution looks like this:

define('WP_DEBUG', true);

if (WP_DEBUG) {
  define('WP_DEBUG_LOG', true);
  define('WP_DEBUG_DISPLAY', true);

  // disable friendly non informative error messages
  define('WP_DISABLE_FATAL_ERROR_HANDLER', true);

  $GLOBALS['wp_filter'] = [
    'enable_wp_debug_mode_checks' => [
      10 => [[
        'accepted_args' => 0,
        'function'      => function() { return false; },
      ]],
    ],
  ];
}

error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);

Unfortunately it turns off automatically also deprecated messages from your own code, but what can you do until Wordpress solves its deprecated code fragments.

1
  • This worked perfectly thank you. So stupid that WP doesn't give us an easier way. Commented Jan 11, 2023 at 17:52
0

I went through all the proposed answer on the different topics about Wordpress 6.x and PHP 8.x.

After trying everything, even the mu-plugin not working, I could fix it by setting the error_reporting() at the end of the wp-config.php file because for some reason, after WP_DEBUG and WP_DEBUG_DISPLAY was not enough.

Here is what I added but anything would work: error_reporting(E_ALL ^ E_DEPRECATED);

2
  • Glad you solved it, but you shouldn't still be getting deprecated warnings nowadays though.
    – Rup
    Commented Jan 16 at 13:19
  • 1
    Indeed, I reported it to the plugin author (buddypress)
    – Niko
    Commented Jan 19 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.