DEV Community

Cover image for Hide PHP Notices & Warnings from WP CLI
Kevinleary.net
Kevinleary.net

Posted on Originally published at kevinleary.net

Hide PHP Notices & Warnings from WP CLI

When PHP notices and warnings appear during a WP CLI command issues can happen that can cause scripts from completing properly. They’re very annoying, and make console feedback dirty and difficult to follow.

Notices and warnings will also commonly appear even when your php.ini file is properly setting the value of error_reporting. There are many reasons this can happen, but in all cases you can disable PHP errors and notices while working with the WordPress CLI by making a small adjustment to your wp-config.php file.

Disable WP CLI Notices

Add the following snippet of code to your wp-config.php file before the definition of ABSPATH constant. This will disable/hide PHP notices for WP CLI commands.

// Disable WP CLI notices
if ( !isset( $_SERVER['HTTP_HOST'] ) ) {
  error_reporting( 5107 );
}
Enter fullscreen mode Exit fullscreen mode

This used a bitmask shorthand to define the rules for error types that are shown. 5107 is the equivalent of the following long-form definition:

error_reporting(
  E_ALL 
  & ~E_PARSE 
  & ~E_NOTICE 
  & ~E_USER_NOTICE 
  & ~E_STRICT 
  & ~E_DEPRECATED 
  & ~E_USER_DEPRECATED
);
Enter fullscreen mode Exit fullscreen mode

Hide PHP Notices & Warnings

To hide PHP Warning and PHP Notice errors from appearing, add this snippet to your wp-config.php file before the definition of ABSPATH constant.

// Disable WP CLI notices & warnings
if ( !isset( $_SERVER['HTTP_HOST'] ) ) {
  error_reporting( 4433 );
}
Enter fullscreen mode Exit fullscreen mode

The bitmask used here is the equivalent of the following long-form definition:

error_reporting(E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR);
Enter fullscreen mode Exit fullscreen mode

Bitmask Error Reporting Levels

The examples above use a shorthand error level argument that represents a specific set of rules for error codes. You can adjust the rules and generate a new bitmask with this PHP error reporting level calculator. You can also use the more descriptive syntax like what’s above, it’s up to you.

Common Related Errors

This approach will solve the following common errors shown in a terminal/console when working with the WP CLI.

PHP Notice:  Undefined index: HTTP_HOST in /vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1323) : eval()'d code on line 9
PHP Notice:  Undefined index: REQUEST_URI in /vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1323) : eval()'d code on line 9
PHP Notice:  Undefined index ...
PHP Notice:  Undefined variable ...
PHP Notice:  Undefined offset 0 ...
Enter fullscreen mode Exit fullscreen mode

These references are added just in case some out there is digging for a direct solution, hopefully this helps you find it.

Top comments (0)