What is the difference between using global $current_screen and get_current_screen()?

In your example, there is currently no difference. You get the same object, if there is one. Try it:

global $current_screen;
$current_screen->foo = 1;

$screen = get_current_screen();
$screen->foo = 2;

echo '$current_screen->foo: ' . $current_screen->foo; // 2!

The simple reason: objects are not passed as a copy in PHP.

But: global variables are really bad, because everyone can change them any time. One day far, far away, WordPress might deprecate this global variable. If you are using the function wrapper to get the object, you should be fine. Otherwise, your code might raise notices.

And always check if you get indeed an object. $current_screen->post_type might not exist.

Leave a Comment