Get WP CLI to hide debug warnings and notices in JSON output, same setting as website

Summary: WP CLI can be used to get a JSON output of all the plugins installed on a site, but the output gets corrupted due to warnings although log levels are set to low.

Problem

Unfortunately, I have a site that runs an outdated plugin producing some warnings. They are not shown on the website, as WP_DEBUG is set to false in wp-config.php. The website runs fine, because there are no fatal errors. WP CLI produces the correct JSON results, too, but prepends all output with a particular warning. Actually it’s the same warning twice, the first instance being prepended with PHP Warning:.

The command I’m running

wp plugin list --fields=name,status,update,version,update_version,title --format=json

Current output

PHP Warning:  Declaration of aec_contributor_list::form() should be compatible with WP_Widget::form($instance) in /sites/[redacted]/files/wp-content/plugins/ajax-event-calendar/inc/widget-contributors.php on line 61
Warning: Declaration of aec_contributor_list::form() should be compatible with WP_Widget::form($instance) in /sites/[redacted]/files/wp-content/plugins/ajax-event-calendar/inc/widget-contributors.php on line 61
[{...JSON omitted for brevity...}]

This is my wp --info

OS: Linux 4.15.0-143-generic #147-Ubuntu SMP Wed Apr 14 16:10:11 UTC 2021 x86_64
Shell:  /bin/bash
PHP binary: /usr/bin/php7.4
PHP version:    7.4.3
php.ini used:   /etc/php/7.4/cli/php.ini
MySQL binary:   /usr/bin/mysql
MySQL version:  mysql  Ver 14.14 Distrib 5.7.34, for Linux (x86_64) using  EditLine wrapper
SQL modes:  
WP-CLI root dir:    phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:  phar://wp-cli.phar/vendor
WP_CLI phar path:   /sites/[redacted]/files
WP-CLI packages dir:    
WP-CLI global config:   /sites/[redacted]/.wp-cli/config.yml
WP-CLI project config:  
WP-CLI version: 2.5.0

Attempts at a solution

I read

  • PHP warning are displaying when using WP CLI
  • wp-cli displays php notices when display errors off

And following that, I changed two settings in /etc/php/7.4/cli/php.ini.

  • error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT -> error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
  • log_errors = On -> log_errors = Off

But this only got rid of the first instance of the warning, so that the output looks like this now:

Warning: Declaration of aec_contributor_list::form() should be compatible with WP_Widget::form($instance) in /sites/[redacted]/files/wp-content/plugins/ajax-event-calendar/inc/widget-contributors.php on line 61
[{...JSON omitted for brevity...}]

Question

I have no idea where else I could look and I’m truly wondering why it seems impossible to apply the same error reporting levels to WP CLI that are already being applied to the website itself?

I am aware that I can simply fix the error in the plugin or try to get rid of it (since it probably isn’t secure anymore either…). But just for the sake of knowing a solution to this issue, should it happen again in the future, I’m happy to hear any thoughts.

1 Answer
1

A quick manual solution is to direct all error output to a log file somewhere or even to /dev/null.

With your command this would look like this:

wp plugin list --fields=name,status,update,version,update_version,title --format=json 2> ./cli-command.err.log

If you totally don’t care about the errors, warnings and notices, you could send it to /dev/null like this:

wp plugin list --fields=name,status,update,version,update_version,title --format=json 2> /dev/null

Having said this, there is some chance that the plugin that you are still getting warnings from is actually using something different than the errors stream. My suggested solution will not help you, if ajax-event-calendar is using echo instead of error_log, but nothing will be better of a solution then than patching the plugin and notifying its author.

Leave a Comment