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)
- Remote work – pros and cons - 13 May 2019
- OptiPic.io – How and why to optimize the image on the site? - 8 April 2019
- Guest post at WP doin! - 26 March 2019