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 );
}

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
);

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 );
}

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);

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.

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 ...

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

Meet the Author

Kevin Leary, WordPress Consultant

I'm a freelance web developer and WordPress consultant in Boston, MA with 17 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.