Quick Tip: When You Can’t Get the Current Screen

[ad_1]

get_current_screen is a really useful WordPress function; unfortunately, it’s not always available depending on how early you may need it

This doesn’t mean there aren’t alternative ways to detect what screen we’re on, though. It just takes some creative conditionals given what both WordPress and PHP provide.

When You Can’t Get the Current Screen

There are a few caveats to this strategy (as-in in may not work for every case), but it’s something worth considering should you need to hook into the WordPress life cycle.

For example, let’s say you need to do something the the screen where you edit the post but you have to do it at a time during which the get_current_screen object isn’t available.

What then?

Why not look at the URL and/or the query string variables available in PHP and then use one of the filter_input functions to check and see. Case in point:

add_action('admin_init', function () {
    if (0 < intval(filter_input(INPUT_GET, 'post'))) {
        // TODO ...
    }
});

From here, you know you’re working on a post or a custom post type and you’re making sure that it’s valid because you’re verifying the ID is greater than 0.

Again, this may not work in all circumstances, but it’s certainly one option that we have at our disposal when other API functions aren’t yet ready.

[ad_2]

Source link