Call to undefined function called get_current_screen
-
Hi Staff,
I’m a developer of VikWP company and I’m reaching you to report an issue that occurs with almost all our plugins.
Sometimes our plugins need to instantiate an editor within the “init” hook, where the
get_current_screenfunction is not yet available. Here’s the code that’s causing the issue:// @file class-editor.php public function is_post_editor_page() { if ( ! is_admin() ) { return false; } $screen = get_current_screen(); return $screen->parent_base === 'edit'; }Since the
get_current_screenfunction doesn’t exist yet, we strongly suggest you to check that before using it. The example below should be able to fix the reported issue:public function is_post_editor_page() { if ( ! is_admin() ) { return false; } /** * Check whether the get_current_screen function exists * because it is loaded only after 'admin_init' hook. */ if ( ! function_exists( 'get_current_screen' ) ) { return false; } $screen = get_current_screen(); /** * Since the screen might not have been set, we should check * whether it returned NULL, in order to avoid displaying * a 'Trying to get property of non-object' notice. */ return $screen !== null && $screen->parent_base === 'edit'; }We would appreciate if you could enhance the way you check whether the user is currently located on the post editor page, so that our plugins won’t face that issue anymore.
The topic ‘Call to undefined function called get_current_screen’ is closed to new replies.