[wp-hackers] How to determine which admin screen I'm currently viewing

Andrew Nacin wp at andrewnacin.com
Thu Dec 1 18:10:03 UTC 2011

On Wed, Nov 30, 2011 at 4:27 AM, Mike Schinkel
<mikeschinkel at newclarity.net>wrote:

> On Nov 30, 2011, at 12:38 AM, Frank Bueltge wrote:
> > Yes, Mike, thats correct. Current it is an mix from different vars, But
> > Franz search an function or var, that give back the value of all admin
> > pages.
> That's not really possible then. Plugins can add their own calls and it is
> not possible to know what those files do.  Besides plugins, WordPress core
> has not been consistent with such information, although I think Nacin has
> been pushing slowly in the direction of improving consistency with his work
> on the screen object.

Indeed. In 3.3 we fixed a lot of issues with the screen object not being
decorated with enough information, or worse, lying to you.

Assuming that $screen = get_current_screen():

Instead of checking $pagenow for 'post.php' or 'post-new.php', you can
check $screen->base (which will be 'post' for all post types, as it is the
base). You can check $screen->action, which will be 'add' for post-new.php.
Instead of checking $pagenow for 'edit.php', get_current_screen()->base
will return 'edit'.

The best way to determine what post type or taxonomy you are viewing is to
check $screen->post_type, $screen->taxonomy (instead of $typenow and
$taxnow, or checking $_GET, etc.). You can obviously combine these to check
both base and post_type and what not, to get a very clear picture of the
page you're trying to attach something to (for example). And of course
there is $screen->id which pulls together the base along with the post type
or taxonomy, depending on whether you are on edit.php, post(-new)?.php,
edit-tags.php, or elsewhere.

There's a lot of good stuff here. I would dig in.


More information about the wp-hackers mailing list