.
WORDPRESS PLUGINS ABOUT

Calling a Function from another WordPress plugin

1 minute read

Let's say you have two plugins installed in WordPress and you need to run a function in PluginB from PluginA. This code will allow you to do this:

Code for PluginB

Here we are calling the custom action created in PluginA called plugin_a_custom_action. This code will check that PluginA is active and then get the data from the function in plugin_a_custom_action_data.

add_action('plugin_a_custom_action', function() {});

Add the following code where you would like the action to run:

if ( is_plugin_active('ave-analytics/ave-analytics.php' ) ) {
    global $plugin_a_custom_action_data;
    do_action('plugin_a_custom_action');
}

Code for PluginA

This is where the function is which you would like to run.

add_action( 'plugin_a_custom_action', [ $this, 'plugin_a_function' ] );

Below is the function you wish to run:

public function plugin_a_function() {
    global $plugin_a_custom_action_data;
    $plugin_a_custom_action_data = "hello";
}
^