You don’t have to say it, I will: we’ve been bad at posting regularly here at NinjaForms.com. It’s not that we haven’t wanted to create great content for you to read, we’ve just been really busy making Ninja Forms more awesomer. Don’t fear, however! New, amazing content will be coming your way next week from our new editor-in-chief, Quay Morgan.Anything that you want to do during processing of Ninja Forms can be handled by adding a new action type.Click To Tweet
I’ve decided to kick off this new content by talking about something that we’re very proud of: the Ninja Forms action system. It drives all Ninja Forms submission processing, and it is one of the most exciting tools for developers offered by Ninja Forms. Anything that you want to do during processing can be handled by adding a new action type. This action type can then be added by the user to any form, right alongside Success Messages, Emails, and Redirects. This system gives you complete control over what your form does during processing. No other WordPress form builder gives you that kind of flexibility.
The action system has a few great benefits:
- It creates a better user experience by centralizing where form processing stuff is setup.
- Action objects allow for code to be neatly organized into a single class.
- New actions automatically inherit support for conditional actions if the user has the Conditional Logic extension.
To create an action, all we have to do is:
- Extend the base action class.
- Add the action type using a WordPress filter.
Let’s break that down a bit further. When we extend the base action class, we inherit all the system stuff that actions need to work, so we just need to overwrite some of the parent methods:
- Our __construct() method provides the nicename for our action type.
- We specify an edit_screen() method that displays the settings for our user when they are setting up the action.
- We create a process() method that handles the processing for our action, and is called, you guessed it, when the action is processing.
That’s it! There are some optional methods we can supply, like save_admin(), but for most action types, that’s all we need to do. Below, we’ll go over each of these steps in some code examples.
Extending the base action class
This is what your actual action class would look like:
And then, you’d just add your custom field using a WordPress filter: