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

I have a basic plugin in the admin options panel for events.

I want to call a function that is from a different social media plugin (Mingle). I can call the plugin functions in the theme, but not within core files of other plugins, or other plugins’ admin panels it seems.

Read More

I have tried including the other plugin’s files, adding a hook to the theme functions file, add_action, add_filter, init etc. But I do not know what the first argument for those actions would be, since I want the function inserted into a specific admin page from another plugin.

The only solution that has worked is merging the code of the two plugins, although it seems odd that there is not a simpler method.

Related posts

Leave a Reply

2 comments

  1. 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.

  2. To call one function from one plugin in another plugin (inception?), you can try the following login:

    You can try by checking for the function you want before doing anything else.

    Plugin A:

    function from_plugin_alpha( some_thing ) {
        // do stuff
    }
    

    Plugin B:

    function from_plugin_bravo() {
        if ( !function_exists( 'from_plugin_alpha' ) ) {
            return;
        }
        from_plugin_alpha( some_thing );
    }
    

    This checks that function exists before executing your custom code.