When adding a function to "admin_enqueue_scripts" a hook is passed to the function. It looks like this "toplevel_page_nameofyourpage". That works great, but how is it different from checking the $_GET variable to see which page you are on? Is one preferred over the other? Why?

I'm only talking about when you create your own menu, not the default admin pages.


The advantage of using the value provided by Wordpress in the hook variable is future compatibility with Wordpress Versions and not having to worry about $_GET variable manipulation by way of the user or another Wordpress plugin.

Checking the $_GET array works, but can be unreliable especially when other poorly coded plugins start messing with the $_GET array, because it is not a global constant and can be modified.

  • that's a good point – John Feb 5 '13 at 20:01

If you use $_GET['page'] you could have a conflict between, for example, /wp-admin/options-general.php?page=some-page (a page under the settings menu) and /wp-admin/admin.php?page=some-page (a page under a menu created by a plugin or theme).

This isn't a problem when using the hook because the hook includes the name of the parent menu.

So if $_GET['page'] isn't a good option, why not use $_SERVER['REQUEST_URI']?

You should be able to do this. I assume that the WordPress developers already had the $hook variable lying around and thought it would be a more straightforwards approach to check what the hook equals than to check what $_SERVER['REQUEST_URI'] starts with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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