I have written a custom plugin that use a specific 3rd party plugin code, so I want to declare my plugin depends on this 3rd party plugin, is it possible? What would be normal approach to this issue?
-
What has been your research so far? Sharing your research helps everyone. Tell us what you found and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and above all, it helps you get a more specific and relevant answer. More info about how to ask a good question can be found on the How to Ask page.– kaiserCommented Oct 28, 2013 at 14:12
1 Answer
It's not possible in the core. There are a few ways to approach this.
1. Hook into plugins_loaded
check to see if X
from your dependency plugin exists.
add_action('plugins_loaded', 'wpse120377_load');
function wpse120377_load()
{
if (!class_exists('Some_Class_From_Another_Plugin')) {
// dependency not installed, bail
return;
}
// load the rest of your plugin stuff here
}
Hooking into plugins_loaded
is important: every plugin that's installed will be loaded by that point. It's only at (or after) plugins loaded fires that you can do an accurate check for a dependency.
2. See if the Plugins Provides Hooks
Take Posts 2 Posts. It provides a hook called p2p_init
that it fires when it's loaded. If your dependency plugin does something like that, you don't need to hook into plugins_loaded
and do a check. Simply hook into the plugins init
action (or whatever) and load your functionality from there.
add_action('p2p_init', 'wpse120377_load2');
function wpse120377_load2()
{
// load your plugin
}
3. The "Nice-to-the-User" Way
If your dependency doesn't exist, your plugin can't function. So be nice to the user and show an error message. admin_notices
is a good hook with which to do that.
add_action('plugins_loaded', 'wpse120377_load3');
function wpse120377_load3()
{
if (!class_exists('Some_Class_From_Another_Plugin')) {
// dependency not installed, show an error and bail
add_action('admin_notices', 'wpse120377_error');
return;
}
// load the rest of your plugin stuff here
}
function wpse120377_error()
{
?>
<div class="error">
<p>
<?php _e('{PLUGIN NAME} requires {ANOTHER PLUGIN}. Please install it.', 'wpse'); ?>
</p>
</div>
<?php
}