wp enqueue script - "admin_enqueue_scripts" hook or $_GET['page']?

When adding a function to "admin_enqueue_scripts" a hook is passed to the function. It looks like this "t

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.

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.

Share Improve this question asked Feb 5, 2013 at 17:40 JohnJohn 3351 gold badge3 silver badges9 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744668437a4586892.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信