0

I am creating a plugin which will also have a few addons. I am adding settings tabs for the addons, but I want to make a conditional check in the main plugin to prevent those tabs from showing if the addon plugin is not active.

There is the function is_plugin_active() which can be used easily for this check, but what if the user decides to change the folder name of the plugin? Then the check will not work correctly? Should I implement some kind of other more robust check?

4
  • I think this was answered here, wordpress.stackexchange.com/questions/54742/…
    – Wyck
    Commented Mar 8, 2013 at 3:38
  • The function I mentioned is a shortcut for what's mentioned in that reply, but we still have a problem if the user changes the core plugin's folder name.
    – urok93
    Commented Mar 8, 2013 at 3:45
  • Not a built in function but rather a hint of using alternative checks - if the addons are created by yourself you can also either use if_function_exists() (or class) , or you can set global var on activation .
    – krembo99
    Commented Mar 8, 2013 at 3:49
  • Would if_function_exists() also be true if the addon plugin is installed but not activated?
    – urok93
    Commented Mar 8, 2013 at 3:55

2 Answers 2

6

There is the function is_plugin_active() which can be used easily for this check, but what if the user decides to change the folder name of the plugin?

Simply put, you're doing it wrong.

If you're making extensions for a plugin, then you should provide hooks in the "parent" plugin for those addons to hook into and change things. Take tabs, for instance, you could use a filter.

<?php
$tabs = apply_filters('myplugin_options_tabs', array(
    'some_tab'  => __('The Tab Label', 'your-textdomain'),
    'a_tab'     => __('Another Label', 'your-textdomain'),
));

foreach ($tabs as $tab => $label) {
   printf('<a href="#%s">%s</a>', esc_attr($tab), esc_html($label));
}

// some where further on where the tab content is supposed to display:
do_action('myplugin_tab_' . some_function_that_gets_the_current_tab() . '_display');

Doing it this way means you don't have to do any checks in your parent plugin. Add ons can simply hook in and do their thing. Much more elegant, and a lot more flexible.

That said, if you really really want to check for the existence of another plugin, the best bet is to hook into plugins_loaded and check for the existence of one of that plugin's classes, functions and/or constants.

<?php
add_action('plugins_loaded', 'wpse89926_load');
function wpse89926_load()
{
   if (class_exists('Other_Plugins_Class')) {
       // do stuff, the other plugin is in installed
   }

   if (function_exists('a_function_in_the_other_plugin')) {
       // do stuff, the other plugin is in installed
   }

   if (defined('A_CONSTANT_IN_THE_OTHER_PLUGIN')) {
       // do stuff, the other plugin is in installed
   }
}
4
  • @christguitarguy Thanks, I get this. However, I would still like to know the method to check if an addon plugin (or vice-versa, the core plugin) is installed and activated, or not. Somebody mentioned using if_function_exists(), and I'm also thinking that checking for a defined constant would be another way to do it.
    – urok93
    Commented Mar 8, 2013 at 16:16
  • fair enough. Added an edit to show what you could do. Commented Mar 8, 2013 at 16:51
  • Excellent, so those checks would be true if the plugin is installed AND active, right?
    – urok93
    Commented Mar 8, 2013 at 17:07
  • correct. You wait until the plugins_loaded hook to check so you can be sure it's loaded. If the plugin is active (and, by extension, install) the checks will be true. Commented Mar 8, 2013 at 18:41
1

chrisguitarguy's solution is the way to go ^^.

Just to add when a plugin is activated the folder/name is stored in the get_option, so using

 get_option( 'active_plugins', array());

will still store the folder/name even if they are changed. If you change the folder or plugin name on an active plugin the plugin will be de-activated, and thus require activation and storing of the correct folder/name.

Though some plugins will break if you do so, and thus not be activated so it's outside your concern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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