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…

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.

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.

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.