5

I'm using Wordpress 3.9.1 and I get the

Fatal error: Call to undefined function get_current_screen()

if I put the following in my funcitons.php or in any file of my theme:

$screen = get_current_screen();
echo $screen;

Initially when I got the error, I was working on a theme that I'm creating. However, I tried this with few other themes including TwentyFourteen, and I still got the same error. I even deleted my Wordpress database, created a fresh one and re-installed Wordpress, but still it didn't help.

I checked the get_current_screen() function by going to to wp-admin/includes/screen.php and it is there.

I am working on Xampp and I don't have any plugins activated as it is a fresh install. Also in my wp-config.php I haven't changed anything but the 'MySQL settings' (database, username and password) and 'DEBUG mode'.

Any suggestions on how to rectify this?

1
  • Please edit more of your code into the question so I can see what you are trying to accomplish.
    – s_ha_dum
    Commented May 27, 2014 at 13:21

3 Answers 3

9

you can try following

if ( !function_exists( 'get_current_screen' ) ) { 
   require_once ABSPATH . '/wp-admin/includes/screen.php'; 
} 
$screen = get_current_screen();
echo $screen;
1
  • 1
    That would include the function, yes, but as the other answer says it won't return any meaningful result (because it's intended to be used in the admin site, and if we were in the admin site then it would already be included).
    – Rup
    Commented Nov 2, 2020 at 13:42
7

get_current_screen() is a backend function and it returns data about the current backend administration page. It makes no sense to use this in theme code. The only way it would work (I think but haven't tested) in functions.php would be if it were hooked into a backend hook later than admin_init.

You don't explain what you are trying to do with get_current_screen() but I think you are simply using it incorrectly.

2
  • You are right, however I don't know which hook to use. According to the Codex, admin_init will not work. Also, I'm trying to use this to check whether the new/edit page of a custom post type is being viewed, so that I can set different defaults for the custom meta boxes that I've created. I'm using same code to create meta boxes for multiple post types, using a plugin. Commented May 27, 2014 at 3:59
  • you need to wrap it in an is_admin() check or run it on admin_init or another admin only hook
    – Tom J Nowell
    Commented Nov 14, 2023 at 13:28
2

maybe your code run before admin_init. Add it to the action:

add_action('admin_init', function(){
  $screen = get_current_screen();
  echo $screen;
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.