Share

WordPress has a powerful plugin API, providing us with a lot of filters and actions throughout. They allow affecting the functionality of WordPress core without actually changing it.

Have you tried Editr WP plugin yet?
It lets you create your own interactive embeds!

What if we wanted to develop something similar in our plugin?

Say we are releasing a GNU GPL plugin, that we constantly update. In case someone altered the behavior of it his changes would easily be overwritten. WP Plugin API saves the day here. We’ll make usage of it and create our own Plugin API for the plugin created.

Let us assume we’re creating a simple plugin which collects user input from a form and saves the data in the database. Say extended user profiles. Storing the values in a custom database table, this could be:

– a list of locations one user has been to

– each location could be stored in one table and user to location relation could be stored in the other

What if we wanted to save the data somewhere else? Send it to a MailChimp campaign (for whatever reason)? Here’s where do_action function comes in handy.

The code above is fairly straightforward – we’re adding the do_action call wherever in our code. And then we’re assigning a hook  to it. The plugin will then execute our hooked code during the process.

This gives the people who browse our code an ability to alter the plugin’s functionality without touching it’s core. That kind of API is part of the biggest plugins out there, for i.a. WooCommerce or Gravity Forms.

 

 

 

The following two tabs change content below.
Working as senior WordPress developer with XFive I have been involved in more than 500 projects. I'm posting tips and tutorials around this blog as well as gathering clients.