- The first plugin, the base, calls
do_action()
or apply_filters()
.
- The second plugin, the one relying on the the first, uses
add_action()
or add_filter()
.
Basic example
Base plugin
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: Base
* Plugin URI: http://wordpress.stackexchange.com/q/112075
*/
class Plugin_Base {
public function __construct()
{
add_action( 'wp_loaded', array ( $this, 'plugin_setup' ) );
}
public function plugin_setup()
{
$headers = get_file_data(
__FILE__,
array(
'name' => 'Plugin Name',
'uri' => 'Plugin URI'
)
);
$data = new stdClass;
$data->plugin_name = $headers[ 'name' ];
$data->plugin_uri = $headers[ 'uri' ];
$data->plugin_file = __FILE__;
do_action( 'plugin_base_loaded', $data );
}
}
new Plugin_Base;
Note the custom action plugin_base_loaded
. This will be the anchor for all depending plugins.
Now let’s create a second plugin that should run after the base has been loaded.
Addon plugin
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: Base Addon
*/
class Plugin_Base_Addon {
protected $data;
public function __construct()
{
add_action( 'plugin_base_loaded', array ( $this, 'setup' ) );
}
public function setup( $data )
{
$this->data = $data;
add_action( 'wp_footer', array( $this, 'wp_footer' ) );
}
public function wp_footer()
{
printf(
'<p>I am an addon for <a href="%1$s">%2$s</a> which lives in <code>%3$s</code>.</p>',
esc_url( $this->data->plugin_uri ),
esc_html( $this->data->plugin_name ),
esc_html( $this->data->plugin_file )
);
}
}
new Plugin_Base_Addon;
This second plugin will not do anything if the base is not active. And if it is active, it will wait until the base has been loaded. Then it will print information about the base into the front-end footer.
You can see a broader example in my article Initialize a plugin with a configuration object, code examples, including an additional plugin, are on GiHub.