I Doubled my Income in a Year

WordPress allowed me to get a better job & better projects in just a Year. I will give you tips, tricks and guide you into WordPress development so you can get more income. Sign up to my 7-day email course!

Building with same levels with article text over it

5 Ways to Make your WordPress Plugin Really Extensible

Creating a WordPress plugin can be really fun but you might create a plugin that other developers can’t extend. There will be clients that need something slightly different that you have made. Create extensible plugins so that other developers can jump in and add their own features. In this article, we will go through five ways which you can use today to make your plugin extensible.

1. Using Theme Support Feature

What is a theme support feature? When you create a theme, you must define that your theme supports a feature. This feature can be the featured image. Adding theme support is done by the theme and you can read more about that function add_theme_support here.

But how to use that within a plugin?

Have you ever had a project where you just wanted to remove some of the styles from a plugin? And for some reason, that was not possible, right? Well, you can make that possible within your plugin!

We can decide to enqueue our style only if the current theme does not support our plugin styles. If the theme was made with our plugin in made and has the necessary styles defined, the theme could have something like this in their code:

But this won’t do any good if we don’t enable such support in our own plugin. So, to enable this we need to enqueue our styles like this:

The function current_theme_supports will return true if the current theme has defined the support with the function add_theme_support. If the theme does not support our plugin’s style, we will enqueue the default plugin styles.

2. Overridable Templates

Templates are a way of adding your own layout for plugin sections. To make your plugin more extensible, you can also provide a way for other developers to overwrite your templates. This can be done by using the filter template_include.

The variable $file is actually a checker variable. If this variable does not get populated and remains false, we will return the original $template. After that, we check if we are on our own custom post type page or on a page that is retrieved from the settings.

You should create your own templating system and implement it here. Those two scenarios are only to show how you can populate the variable $file.

If it’s populated, we then check for the template in the current theme. Since we have added my-plugin/templates/, this folders should be in the theme. Otherwise, this search will return false.

If there is no such template in the current theme, we are building the path to the file in our plugin folder. To see some other ways on creating overridable templates, you should check my other article: How to Include or Override WordPress Templates.

You should check popular plugins and find out how they do it to get an idea.

WooCommerce has its own templating system built in the class WC_Template_Loader which you can find in includes/class-wc-template-loader.php. If you are interested into learning about templating, I do advise you to look the code in that file.

Another interesting approach can be found in SportsPress in their class SP_Template_Loader and you can read the code in the file includes/class-sp-template-loader.php.

3. Apply Filters

To make your plugin extensible you should also make use of the WordPress Plugin API. We will now go over on how to use the filter hook and in the next section, you’ll also see how to use actions.

A great example on why the filters are useful in your own plugin is the example we did before. If WordPress core did not include the filter template_include, we would not be able to change the template.

When we hook our functions to other filters, we are not making our plugin extensible. We are just extending the other plugin, theme or core feature.

Our plugin will be extensible if we apply filters on our own values. We can do that by using the function apply_filters. You can find out more about it on WordPress Developer Resources. You can pass as many parameters as you want to apply_filters, but only the first one will be returned as the value.

Other parameters are passed only for other functions to use them so that they can extend them. Let’s see an example from the plugin Easy Digital Downloads. This plugin uses the function apply_filters to filter the price for the item in the cart.

The other functions are not from EDD, but only for presentation purposes. How functions will be hooked is not your concern, your concern is just to make it possible.

4. Do Actions

Actions are similar to filters when making a plugin extensible. They do not have a function such as apply_filters since they don’t need to filter anything and return data.

They are used for creating events after or before something happens. For that, you will use the function do_action. To read more about that function, check the WordPress Developer Resources.

In summary, you pass an action name and that’s it. If you want other functions to receive more data, you can pass some parameters.

I will use Easy Digital Downloads again for this example. We will see how this plugin set up the action after a purchase has been completed.

Once a purchase has been completed, you can hook in with your own function and do whatever you want. You have the $payment_id, so you can retrieve payment information, connect to a 3rd Party API and collect data or whatever you need.

In my eBook “Easy Digital Downloads for Developers”, I am using that hook to change statuses for my dynamic product, redirect the user, etc.

 

5. Shortcodes

Shortcodes? I know, it feels like it does not belong here. But shortcodes provide us with a great way of display data in posts, pages, etc. It also makes other non-tech users able to show many complex data in their pages.

In general, shortcodes are already extensible with their attributes. You can add as many attributes as you want to a shortcode to display various data. The attributes are predefined inside the code, but that does not make a plugin extensible.

But, did you know that you can make those attributes pretty extensible with just a simple change in the code?

Just by adding the third parameter, WordPress will call the filter shortcode_atts_{$shortcode} where {$shortcode} is replaced with the third parameter.

You can also add a filter hook before returning the output of a shortcode. That way, if there is a need, a developer can change the output of your shortcode. Action hooks can be also useful. With actions, other developers can add their own piece of content.

Conclusion

If you are writing your own plugin, then use some of those approaches to make it your plugin extensible. Don’t get tricked into thinking you’ll never need such a thing. You really might and you can’t know when that will happen.

WordPress is really powerful and it is built around the Plugin API so that you can easily make your own plugin extensible or just extend something else.

Have you tried making your plugin extensible? Or did you just create features by extending other plugins? Share with me and others you experience in the comments below.

About the Author Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

follow me on:

EDD for Developers

In less than 2 weeks, I have earned $1,800 by working on a custom solution with Easy Digital Downloads without any prior experience. Let me teach you how you can too with various custom solutions.

Get the eBook

Leave a Comment:

3 comments
Maksim says May 2, 2017

Thanks, very interesting post.

Reply
George says June 22, 2017

Any tutorial that uses the vague “foo” and “bar” is just lame.

Why not give real practical and thus understandable examples?!

Reply
    Igor Benic says June 22, 2017

    Hi George, as this is not a tutorial on how to create a shortcode, I did not go into real shortcode examples. I have just added that so people can see how they can make the shortcode be extensible. Replace foo & bar with ID & class and you have a shortcode that can get a post from the ID and apply a class to the HTML rendering the post.

    Reply
Add Your Reply

5 Ways to Make your WordPress Plugin Really Extensible

by Igor Benic time to read: 5 min
3