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.
Latest posts by Rafał Gicgier (see all)
- A Gutenberg WordPress 5.0 plugin review. Is it worth it? - 8 December 2018
Using Widget Base Class
for WordPress - 30 July 2018
- The Pros and Cons of using Timber and gulp for WordPress Theme Development - 18 July 2018