Display a custom error message when `themes/functions.php` fatals by danielbachhuber · Pull Request #5817 · wp-cli/wp-cli
Fixes #5240
Trying to fix this upstream is too much of a rabbit hole. Instead, let's give the user a more helpful pointer:
$ wp --skip-themes option get home
Fatal error: Uncaught Error: Call to undefined function genesis_register_sidebar() in /Users/danielbachhuber/projects/vanilla/wp-content/themes/functions.php:3
Stack trace:
#0 /Users/danielbachhuber/projects/vanilla/wp-settings.php(599): include()
#1 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/WP_CLI/Runner.php(1349): require('/Users/danielba...')
#2 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/WP_CLI/Runner.php(1267): WP_CLI\Runner->load_wordpress()
#3 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php(28): WP_CLI\Runner->start()
#4 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/bootstrap.php(83): WP_CLI\Bootstrap\LaunchRunner->process(Object(WP_CLI\Bootstrap\BootstrapState))
#5 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/wp-cli.php(32): WP_CLI\bootstrap()
#6 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/boot-fs.php(17): require_once('/Users/danielba...')
#7 {main}
thrown in /Users/danielbachhuber/projects/vanilla/wp-content/themes/functions.php on line 3
Error: An unexpected functions.php file in the themes directory may have caused this internal server error.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to say it's quite the edge case but alas 🤷♂️
| if ( ! empty( $error_data['error']['file'] ) | ||
| && false !== stripos( $error_data['error']['file'], 'themes/functions.php' ) ) { | ||
| $text_message = 'There was an internal server error that may have been caused by an unexpected functions.php file in the themes directory.'; | ||
| $text_message = 'An unexpected functions.php file in the themes directory may have caused this internal server error.'; |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, what about the original error message? Shouldn't that be kept and displayed as well?
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@schlessera I don't think the original error message is particularly helpful.
Original output
$ wp --skip-themes option get home
Fatal error: Uncaught Error: Call to undefined function genesis_register_sidebar() in /Users/danielbachhuber/projects/vanilla/wp-content/themes/functions.php:3
Stack trace:
#0 /Users/danielbachhuber/projects/vanilla/wp-settings.php(600): include()
#1 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/WP_CLI/Runner.php(1349): require('/Users/danielba...')
#2 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/WP_CLI/Runner.php(1267): WP_CLI\Runner->load_wordpress()
#3 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php(28): WP_CLI\Runner->start()
#4 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/bootstrap.php(83): WP_CLI\Bootstrap\LaunchRunner->process(Object(WP_CLI\Bootstrap\BootstrapState))
#5 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/wp-cli.php(32): WP_CLI\bootstrap()
#6 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/boot-fs.php(17): require_once('/Users/danielba...')
#7 {main}
thrown in /Users/danielbachhuber/projects/vanilla/wp-content/themes/functions.php on line 3
Error: There has been a critical error on this website.Learn more about troubleshooting WordPress. There has been a critical error on this website.
New output
$ wp --skip-themes option get home
Fatal error: Uncaught Error: Call to undefined function genesis_register_sidebar() in /Users/danielbachhuber/projects/vanilla/wp-content/themes/functions.php:3
Stack trace:
#0 /Users/danielbachhuber/projects/vanilla/wp-settings.php(600): include()
#1 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/WP_CLI/Runner.php(1349): require('/Users/danielba...')
#2 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/WP_CLI/Runner.php(1267): WP_CLI\Runner->load_wordpress()
#3 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php(28): WP_CLI\Runner->start()
#4 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/bootstrap.php(83): WP_CLI\Bootstrap\LaunchRunner->process(Object(WP_CLI\Bootstrap\BootstrapState))
#5 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/wp-cli.php(32): WP_CLI\bootstrap()
#6 /Users/danielbachhuber/projects/wp-cli-dev/wp-cli/php/boot-fs.php(17): require_once('/Users/danielba...')
#7 {main}
thrown in /Users/danielbachhuber/projects/vanilla/wp-content/themes/functions.php on line 3
Error: An unexpected functions.php file in the themes directory may have caused this internal server error.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters