wp_debug_mode()WP-CLI 1.0

Sets error handling settings in PHP based on the constants specified in the wp-config.php file of WordPress.

This function is called in the core at a very early stage (before many files are included and before the hook mu_plugin_loaded). It is not intended for direct use anywhere.

Uses three constants:

  • WP_DEBUG (default false)
  • WP_DEBUG_DISPLAY (default true)
  • WP_DEBUG_LOG (default false)

All of them are defined by the early function wp_initial_constants() and can be defined in the wp-config.php file.

WP_DEBUG

If you enable WP_DEBUG (set the value to true), all PHP errors (notices and warnings) will be displayed on the screen. WordPress will also show internal notices that are created by WordPress itself when deprecated functions or deprecated function variables are used or when variables are used incorrectly. Deprecated functions may be removed in later versions of WordPress.

Important! Disabling WP_DEBUG does not disable the display of all types of errors! That is, when WP_DEBUG is disabled, the following types of errors will still be shown (handled):

  • E_CORE_ERROR
  • E_CORE_WARNING
  • E_COMPILE_ERROR
  • E_ERROR
  • E_WARNING
  • E_PARSE
  • E_USER_ERROR
  • E_USER_WARNING
  • E_RECOVERABLE_ERROR

So, for example, if the server is set by default to show errors and WP_DEBUG is disabled, you will still see errors of the types listed above, such as WARNING.

Enabling WP_DEBUG does not change the value of other constants. That is, if you turn off WP_DEBUG, WP_DEBUG_DISPLAY and WP_DEBUG_LOG will retain their default values, and based on these values, PHP settings for error display and logging will be set.

Developers are strongly encouraged to enable WP_DEBUG when creating themes and plugins.

More about WP_DEBUG.

WP_DEBUG_DISPLAY

When WP_DEBUG_DISPLAY = true, WordPress displays errors on the screen.

If specified in wp-config.php:

  • define( 'WP_DEBUG_DISPLAY', true ) — (by default) WP will output (show) errors on the screen.
  • define( 'WP_DEBUG_DISPLAY', null ); — then WP will not specify a value for the PHP option display_errors at all, i.e., the global PHP (server) setting will be used.
  • define( 'WP_DEBUG_DISPLAY', false ); — then error display will be disabled.

Error display is always disabled for REST, AJAX, or XML-RPC requests. For them, the following code ini_set( 'display_errors', 0 ) is triggered, but the value of the constant WP_DEBUG_DISPLAY does not change!

WP_DEBUG_LOG

If you enable WP_DEBUG_LOG, errors will be logged to the debug.log file in the content folder, usually wp-content.

Starting from version 5.1, the value of this constant can be passed the path to the log file where errors should be recorded.

No Hooks.

Returns

null. Nothing.

Usage

wp_debug_mode();

Examples

0

#1 There are no examples.

This function is used by the kernel for error management and is not intended to be used in development.

wp_debug_mode() code WP-CLI 2.8.0-alpha

function wp_debug_mode() {
	if ( WP_CLI::get_config( 'debug' ) ) {
		if ( ! defined( 'WP_DEBUG' ) ) {
			define( 'WP_DEBUG', true );
		}

		error_reporting( E_ALL & ~E_DEPRECATED & ~E_STRICT );
	} else {
		if ( WP_DEBUG ) {
			error_reporting( E_ALL );

			if ( WP_DEBUG_DISPLAY ) {
				ini_set( 'display_errors', 1 );
			} elseif ( null !== WP_DEBUG_DISPLAY ) {
				ini_set( 'display_errors', 0 );
			}

			if ( in_array( strtolower( (string) WP_DEBUG_LOG ), [ 'true', '1' ], true ) ) {
				$log_path = WP_CONTENT_DIR . '/debug.log';
			} elseif ( is_string( WP_DEBUG_LOG ) ) {
				$log_path = WP_DEBUG_LOG;
			} else {
				$log_path = false;
			}

			if ( false !== $log_path ) {
				ini_set( 'log_errors', 1 );
				ini_set( 'error_log', $log_path );
			}
		} else {
			error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR );
		}

		if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
			ini_set( 'display_errors', 0 );
		}
	}

	// XDebug already sends errors to STDERR.
	ini_set( 'display_errors', function_exists( 'xdebug_debug_zval' ) ? false : 'STDERR' );
}