Wordpress - Fixing the White Screen of Death

  • Posted on: 11 August 2015
  • By: davis

There are lots of basic guides to getting around Wordpress's infamous WSOD (White Screen of Death). When you navigate to your Wordpress site and it simply shows a white screen, get ready to debug!

Here is a way to make this less painful. I appreciate this snippet I found on Github: https://github.com/stracker-phil/wp-no-white-screen/

I submitted a pull request to fix some of the language in the Readme, but this plugin is invaluable! Saves a huge headache.

Here's the gist of it - put this plugin in /wp-content/mu-plugins

 

init();
		// Make sure to use THIS error handler after any action was fired.
		add_action( 'all', array( $this, 'init' ), 1 );
		add_action( 'all', array( $this, 'init' ), 9999 );
	}
	public function process_exception( $exception ) {
		$this->dump(
			$exception->getMessage(),
			'Exception',
			$exception->getTrace(),
			$exception->getFile(),
			$exception->getLine()
		);
	}
	public function process_error( $errno, $errstr, $errfile, $errline ) {
		switch ( $errno ) {
			case E_STRICT:
			case E_NOTICE:
			case E_DEPRECATED:
			case E_USER_NOTICE:
				$type = 'notice';
				$fatal = false;
				$color = '#0AD';
				break;
			case E_WARNING:
			case E_USER_WARNING:
				$type = 'warning';
				$fatal = false;
				$color = '#EA0';
				break;
			default:
				$type = 'fatal error';
				$fatal = true;
				$color = '#F00';
				break;
		}
		$trace = debug_backtrace();
		$this->dump( $errstr, $type, $trace, $errfile, $errline, $color );
		if ( $fatal ) {
			echo '

Fatal error. Terminate request!

'; exit( 1 ); } } private function dump( $message, $type, $trace, $err_file = false, $err_line = false, $color = '#AAA' ) { if ( ! empty( $err_file ) ) { $file_pos = "In $err_file [line $err_line]"; } else { $file_pos = ''; } if ( php_sapi_name() == 'cli' ) { if ( ! empty( $file_pos ) ) { $file_pos = "\n" . $file_pos; } echo 'Backtrace from ' . $type . ' "' . $message . '"' . $file_pos . "\n"; foreach ( $trace as $item ) { echo ' ' . (isset($item['file']) ? $item['file'] : ''); echo ' ' . (isset($item['line']) ? $item['line'] : '') . ' '; echo 'calling ' . $item['function'] . '()' . "\n"; } } else { if ( ! empty( $file_pos ) ) { $file_pos = '
' . $file_pos; } $style_list = array( 'padding' => '1px 10px', 'border-left' => '5px solid ' . $color, ); $styles = ''; foreach ( $style_list as $name => $value ) { $styles .= $name . ':' . $value . ';'; } echo '
'; echo '

' . "\n"; echo ' ' . $message . '
' . "\n"; echo ' Backtrace from ' . $type . $file_pos . ':' . "\n"; echo '

    ' . "\n"; foreach ( $trace as $item ) { echo '
  1. ' . (isset($item['file']) ? $item['file'] : ''); echo ' [line ' . (isset($item['line']) ? $item['line'] : '?') . '] '; echo 'calling ' . $item['function'] . '()
  2. ' . "\n"; } echo '
' . "\n"; echo '

' . "\n"; } if ( ini_get( 'log_errors' ) ) { $items = array(); foreach ( $trace as $item ) { $items[] = (isset($item['file']) ? $item['file'] : '') . ' ' . (isset($item['line']) ? $item['line'] : '') . ' calling ' . $item['function'] . '()'; } $message = 'Backtrace from ' . $type . ' "' . $message . '"' . $file_pos . '' . join( ' | ', $items ); error_log( $message ); } while ( ob_get_level() ) { ob_end_flush(); } flush(); } public function init() { if ( defined( 'WP_DEBUG_CORE' ) && ! WP_DEBUG_CORE ) { return; } error_reporting( E_ALL ); // Not sure if this is needed, but we'll add it! set_error_handler( array( $this, 'process_error' ) ); set_exception_handler( array( $this, 'process_exception' ) ); } } if ( ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ( defined( 'WP_DEBUG_CORE' ) && WP_DEBUG_CORE ) ) { No_White_Screen_Of_Death::instance(); }