wp admin - Is it save to require plugin.php early to be able to use get_plugin_data() earlier?

admin2025-06-02  1

I'd like to avoid hard coding my plugin's version at multiple places. To realize this the function get_plugin_data() comes in handy. But here the fun comes to an unpleasant stop. Checking for a plugin update to execute related housekeeping for example should be done early (e.g. plugins_loaded) but unfortunately wp-admin/includes/plugin.php is not loaded before admin_init is fired.

Using a later hook is not possible here. This might be a solution for some plugins but it doesn't work for me, as my plugin hooks into the login process and needs to do its on update processing before that...

My question is: is it save to require( ABSPATH . 'wp-admin/includes/plugin.php') earlier in my plugin? The file contains some function definitions and adds a filter. That's it.

I tried it. At first glance it works and I cannot see any implications but maybe you are aware of some? I'd be happy about any hint, positive or negative. Thank you!

I'd like to avoid hard coding my plugin's version at multiple places. To realize this the function get_plugin_data() comes in handy. But here the fun comes to an unpleasant stop. Checking for a plugin update to execute related housekeeping for example should be done early (e.g. plugins_loaded) but unfortunately wp-admin/includes/plugin.php is not loaded before admin_init is fired.

Using a later hook is not possible here. This might be a solution for some plugins but it doesn't work for me, as my plugin hooks into the login process and needs to do its on update processing before that...

My question is: is it save to require( ABSPATH . 'wp-admin/includes/plugin.php') earlier in my plugin? The file contains some function definitions and adds a filter. That's it.

I tried it. At first glance it works and I cannot see any implications but maybe you are aware of some? I'd be happy about any hint, positive or negative. Thank you!

Share Improve this question edited Jun 20, 2015 at 0:57 wedi asked Jun 19, 2015 at 19:13 wediwedi 1414 bronze badges 4
  • Why isn't WordPress handling your plugin updates? – s_ha_dum Commented Jun 20, 2015 at 0:07
  • Of course it's handling the plugin updates but it doesn't know what to do when I change data formats or the like. ;) – wedi Commented Jun 20, 2015 at 0:29
  • I really don't understand that response. – s_ha_dum Commented Jun 20, 2015 at 0:39
  • WordPress updates my plugin like any other. But sometimes you need to do some housekeeping on updates like database scheme changes or the like. Further reading on the topic: make.wordpress/core/2010/10/27/… – wedi Commented Jun 20, 2015 at 0:50
Add a comment  | 

2 Answers 2

Reset to default 3

At first glance it works and I cannot see any implications but maybe you are aware of some?

You say that you are not getting errors, which I would have expected but it looks as though WordPress uses require_once() so you are probably safe:

39  /** WordPress Plugin Administration API */
40  require_once(ABSPATH . 'wp-admin/includes/plugin.php');

This kind of hack tends to indicate that you are doing something wrong though.

No, don't require plugin.php.

To avoid a tiny bit of bad code you are about to introduce a lot of really bad code. Who knows what can go wrong if you include plugin.php. And who knows what could go wrong in the years to come. WordPress core developers are not expecting users to include core files earlier in init process.

So instead of doing it in really dangerous and unexpected way, just do something like define('MY_PLUGIN_VERSION', 2); right below your plugin Version header. This is way safer and better.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748803257a313849.html

最新回复(0)