Fatal error: Uncaught Error: Call to a member function is_block_editor() on null
Describe the bug In our theme we use MerlinWp Wizard and get following error during the activation
Uncaught Error: Call to a member function is_block_editor() on null in E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-content\plugins\gutenberg\lib\widgets.php:13
Stack trace:
#0 E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-includes\class-wp-hook.php(286): gutenberg_block_editor_admin_print_styles('')
#1 E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-includes\class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
#2 E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-includes\plugin.php(465): WP_Hook->do_action(Array)
#3 E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-content\themes\5th-avenue\inc\integrations\merlin\merlin.php(462): do_action('admin_print_sty...')
#4 E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-content\themes\5th-avenue\inc\integrations\merlin\merlin.php(410): Merlin->header()
#5 E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-includes\class-wp-hook.php(284): Merlin in E:\work\openserver\OSPanel\domains\localhost\tests\va-5.1.1-1\wp-content\plugins\gutenberg\lib\widgets.php on line 13
function gutenberg_block_editor_admin_print_styles() {
if ( get_current_screen()->is_block_editor() ) {
/** This action is documented in wp-admin/admin-footer.php */
// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
do_action( 'admin_print_styles-widgets.php' );
}
}
add_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' );
To reproduce Steps to reproduce the behavior:
- Install theme with MerlinWp Wizard
- Skip the Wizard installation
- Switch from Parent Theme to Child Theme to trigger Merlin Wizard
- See error
Expected behavior The reason for this is that the function get_current_screen() returns null. As specified in the documentation for this function
/**
* Get the current screen object
*
* @since 3.1.0
*
* @global WP_Screen $current_screen
*
* @return WP_Screen|null Current screen object or null when screen not defined.
*/
You need to add a check if the function get_current_screen() returns the WP_Screen class and then call the method is_block_editor()
Additional context
- Gutenberg 5.5.0
Solution Need to change
function gutenberg_block_editor_admin_print_styles() {
$current_screen = get_current_screen()
if ( ! is_null ( $current_screen ) && $current_screen->is_block_editor() ) {
/** This action is documented in wp-admin/admin-footer.php */
// phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
do_action( 'admin_print_styles-widgets.php' );
}
}
add_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' );
There were similar issues previously as in https://github.com/WordPress/gutenberg/issues/14547#issuecomment-475375258 where get_current_screen will either (a) not be defined as a function or (b) return null when called, depending on whether the current screen is an admin screen. In this specific instance, it may have been expected that since the logic of this function occurs in the action handler of 'admin_print_styles' that certain assumptions about it being an admin screen could be made.
That being said, it seems with the recurring presence of these sorts of errors that we should always be considerate of accounting for the possible failure conditions. This should also apply to all other action handlers of the same file where similar logic occurs.
Sometimes conflicts occur, I tried installing the Classic Editor, which helped.
But it might just be in my case.
@auerserg - This might be a workaround, but you can use set_current_screen() within the class-merlin.php file:
if( is_null ( get_current_screen() )) {
set_current_screen('Merlin');
}
Right after this block -
if ( empty( $_GET['page'] ) || $this->merlin_url !== $_GET['page'] ) {
return;
}
Hi, same issue when opening the setup assistant of WP Security Audit Log plugin.
Related: https://wordpress.org/support/topic/call-to-a-member-function-is_block_editor-on-null/
This is slated to be fixed by #15983.
Fixed in https://github.com/WordPress/gutenberg/pull/15983.