admin管理员组文章数量:1323381
Usually I use this method to test whether a plugin is active (usually my own).
This would be in the plugin I'm checking for:
function my_plugin_setup() {
// setup code here
}
add_action( 'plugins_loaded', 'my_plugin_setup' );
From another file or plugin I would use has_filter
on the function which is fired on plugins_loaded
:
if ( has_filter( 'plugins_loaded', 'my_plugin_setup' ) ) {
// plugin is active, do something
}
How would I use this same method above but for a class based plugin? Ie the plugin code looks something like:
class My_Plugin {
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'setup' ) );
}
// other stuff below
}
Obviously this doesn't work:
if ( has_filter( 'plugins_loaded', array( $this, 'My_Plugin' ) ) ) {
// do something
}
Note: I'd like to use this method instead of using is_plugin_active
because the plugin's folder name might change and hence is_plugin_active would no longer work.
Usually I use this method to test whether a plugin is active (usually my own).
This would be in the plugin I'm checking for:
function my_plugin_setup() {
// setup code here
}
add_action( 'plugins_loaded', 'my_plugin_setup' );
From another file or plugin I would use has_filter
on the function which is fired on plugins_loaded
:
if ( has_filter( 'plugins_loaded', 'my_plugin_setup' ) ) {
// plugin is active, do something
}
How would I use this same method above but for a class based plugin? Ie the plugin code looks something like:
class My_Plugin {
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'setup' ) );
}
// other stuff below
}
Obviously this doesn't work:
if ( has_filter( 'plugins_loaded', array( $this, 'My_Plugin' ) ) ) {
// do something
}
Note: I'd like to use this method instead of using is_plugin_active
because the plugin's folder name might change and hence is_plugin_active would no longer work.
- 1 You mean like this? – fuxia ♦ Commented Jun 30, 2013 at 23:02
2 Answers
Reset to default 1To do literally what you're wanting here, the easiest way is to store the instance of the plugin class in a global variable at the end of your plugin:
class My_Plugin {
static $instance;
function __construct(){} /* or private, with singleton get_instance() method */
function setup(){ ... }
}
$my_plugin_instance = new My_Plugin();
add_action( 'plugins_loaded', array( $my_plugin_instance, 'setup' ) );
Then you can do:
$has_plugin = (
isset( $my_plugin_instance )
&&
has_filter( 'plugins_loaded', array( $my_plugin_instance, 'setup' )
);
if ( $has_plugin ) ...
However, this approach to see if a plugin has been loaded seems overly complicated. Why not just check to see if class_exists( 'My_Plugin' )
? If the class does exist, then you know the plugin was loaded. No need to do any checks for if a filter was added.
In plugins that don't actually create a unique action, a simple way to test for the presence of another plugin with has_filter() is to create a small filter. For instance, in your case, you might create a filter or hook called "my_plugin" (provided the string was unique) via something like this:
add_action( 'my_plugin', '__return_true' );
And then when you want to test that your plugin is both installed and active you could use WordPress's has_filter()
like this:
$has_plugin = has_filter( 'my_plugin' );
...
Obviously it's critical to make sure that the filter/hook doesn't exist elsewhere, by using a suitably unique name. Also, many plugins have an action or hook that is specific to that plugin, in which case that could simply be used for this test rather than creating another filter.
Doing this with has_filter()
has the dual advantages of making the test simple as well as being robust even if the plugin is renamed, or an alternate plugin is used; I imagine this was why it was recommended to you as good practice, and it's what I've always done in my plugins.
I know this is an old post, but it's still a problem that WordPress developers need to solve, so hope this is helpful to someone.
本文标签: functionsUsing hasfilter with a class based plugin to check whether the plugin is active or not
版权声明:本文标题:functions - Using has_filter with a class based plugin to check whether the plugin is active or not 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742121730a2421740.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论