What are Modular Plugins

A modular plugin is a plugin that can be extended or modified without changing the core plugin code. I am fairly new to the WordPress world of development so this was an exciting topic for me. I have been a developer for many years and love the idea of making code extensible.

Modular Plugin Building Blocks

There are a few important functions in WordPress that make our job of writing modular plugins pretty easy. Not all of them have to be used to make your plugin extensible.

Here is a breakdown of each and what the WordPress codex says about each…

  • apply_filters() – call the functions added to a filter hook
  • add_filter() – hooks a function to a specific filter action.
  • has_filter() – check if any filter has been registered for a hook
  • add_action() – hooks a function on to a specific action
  • do_action() – executes a hook created by add_action
  • has_action() – check if any action has been registered for a hook

That’s a mouth full. Let’s see if we can break down what each of those means to us during plugin development.

apply_filters()

In its simplest form apply_filters() provides a way for your plugin to allow other plugins to replace text. For instance, if you have a shortcode to display some text you can use apply_filters() to allow other plugins to change that text.

function mpe_create_shortcode( $atts ) {
	// Set whatever text you would normally display
	$default_text = 'My Fancy Text';

	// Return the apply_filters() function.  It will replace your text
	// with the text from a calling hook
	return apply_filters('mpe_text', $default_text);
}
add_shortcode( 'mpeshortcode', 'mpe_create_shortcode' );

add_filter()

The add_filter() function will allow you to hook into a plugin where ever an apply_filter() has been used. Building on the apply_filters() example above you could modify that shortcode text in your plugin by calling add_filter() against the mpe_title hook.

function mpe_add_a_filter() {
	// Return the new text that will be displayed
	return 'A different fancy text';
}
// Create the hook to mpe_text
add_filter( 'mpe_text', 'mpe_add_a_filter' );

has_filter()

This function is provided as a means to check if the filter you are about to apply already exists.

function mpe_add_a_filter() {
	// Return the new text that will be displayed
	return 'A different fancy text';
}

// Check if the hook exists
if (!has_filter( 'mpe_text', 'mpe_add_a_filter' )) {
	// It doesn't exist so create the hook to mpe_text
	add_filter( 'mpe_text', 'mpe_add_a_filter' );
}

do_action()

This function is used to set entry points, or hooks, into your plugin. This is handy as it allows not only you, but other plugins to inject code at a specific point within your code.

function do_something() {
	// Create a hook for other functions/plugins to
	// perform an action whenever this function is called
	do_action('do_something_else');
}

add_action()

The add_action() function is probably the most used function in this group. It is used to hook a function in your plugin to an action hook in another plugin defined by the do_action function. WordPress itself has many action hooks that can be used to perform various functions at different points within WordPress. You can find a list of WordPress action hooks in the WordPress codex.

function another_something() {
	// Perform some action here;
}
// Perform the 'another_something' function in
// place of the do_action('do_something_else')
add_action('do_something_else', 'another_something');

has_action()

Similar to the has_filter() function has_action() is used to determine if an action is already in use before it is added.

function another_something() {
	// Perform some action here;
}

// Check if the action exists
if (!has_action( 'do_something_else', 'another_something' )) {
	// It doesn't exist so create the hook to do_something_else
	add_action('do_something_else', 'another_something');
}
Posted in Tech Tagged with: , ,

Leave a Reply