How can I call a function from one plugin within another plugin?

Maybe you should try calling the functions of your plugin using the plugins_loaded action.

Plugin A

class PluginA {
  public function func_a() {
    // do stuff
  }
}

Plugin B

class PluginB {
  function functB() {
    if (class_exists('PluginA')) {
      //do stuff that depends of PluginA
    }
  }
}

add_action('plugins_loaded', 'call_plugin_a_using_plugin_b');
function call_plugin_a_using_plugin_b() {
  PluginB::functB();
}

According to the Codex, the plugins_loaded action fires after all plugins are loaded, so making sure that all plugins are loaded before calling a function from other plugin might be the way to go.

Leave a Comment