A simple modular plugin example

This post will illustrate a very simple modular plugin and how to extend it using another plugin. I am going to assume you already know how to create a plugin and not go into a lot of detail stepping through the creation of the plugins we will build in this post.

If you do need a primer on creating plugins refer to How to Create a WordPress Plugin. At any time if you would like to learn more about modular plugins I always recommend visiting the WordPress codex. I have provided an overview of modular plugins in my post What are Modular Plugins. It will also give you a starting point to research some more in the WordPress codex and of course the ever popular Google!

You can find a complete code listing at the end of this post. Keep in mind we will be creating two plugins in this post. The first is the modular plugin that will create a shortcode to display some text. The second is the plugin that will hook into the modular plugin to replace the text that will be displayed.

This is probably not the most practical example you will see, but it does illustrate the point.

Getting Started

To get started we will need to create a folder for our plugin and create the plugin PHP file. I’m going to name mine mpe-modular-example.

Step 1 Create a function to display some text

This is a pretty straightforward function. It adds a shortcode that displays a string of text. The important piece to notice is line 8. On this line we add a hook for other plugins to jump in and replace our text with whatever text they want to display.

function mpe_create_shortcode() {
// This is the text you intend to
// display when the shortcode is used
$default_text = 'My Fancy Text';
// apply_filters allows other plugins to jump in
// and replace your default text with their own
return apply_filters('mpe_text', $default_text);
}
add_shortcode( 'mpeshortcode', 'mpe_create_shortcode' );

That is pretty much the entire plugin. Activate this plugin and create a page or post and type the shortcode [mpeshortcode]. You will see the text “My Fancy Text” appear when viewed.

Next we will create the plugin that hooks into the one we just wrote so that each time the [mpeshortcode] is used different text is displayed.

Step 2 Create second plugin

At this point we will need to create another folder within our plugins folder along with a new plugin PHP file. I’m going to name mine mpe-call-modular-example.

Step 3 Add code to hook into our first plugin

The following code will hook into our first plugin by using the add_filter call.

In this code we simply applied a filter to the mpe_text filter that was created in our first plugin. We passed our mpe_add_a_filter function which returns a string. WordPress now knows that whenever the [mpeshortcode] shortcode is used to replace the text it would normally use with our new text.

Activating the plugin will immediately modify the output of the [mpeshortcode] shortcode.

Summary

As I mentioned before this is probably not the most useful illustration of modular plugin usage, but I do feel it illustrates the power of modular plugins. The ability to allow other writers of plugins to change behavior or modify the output of your plugin without changing your core plugin code is huge.