The Run WordPress Hook action runs a WordPress action or filter when a user saves or submits a form. Although simple this action is powerful and allows developers to run any PHP code when a website visitor submits a form.
Adding This Action
To add a Run WordPress Hook action:
- When editing your form, click the Actions icon at the top of the page. The Actions sidebar will open on the right-hand side of the page.
- Click the Add icon to add a new action.
- Select Run WordPress Hook from the Action pulldown. A series of settings will appear.
As with all sidebars in WS Form, click the Save button at the bottom to save your changes, or click Cancel to disregard your changes.
The Run WordPress Hooks action settings are as follows:
When Should This Action Run?
You can choose to run the WordPress hook when the form is saved, submitted, or both.
Type
Select the type of hook you would like to run:
- Filter (apply_filter)
- Action (do_action)
Hook Tag
Enter the tag name of the filter or action you want to run.
Priority
Choose whether to run the hook before or after other actions. The options are:
- Before submission created Hook will run before any other actions. #submit_id variable will be blank.
- Before other actions Hook will run after the submission is created but before other actions run. #submit_id will be set.
- After other actions Hook will run after all other actions have run.
If you want to manipulate field values before other actions run, select Before submission created or Before other actions.
If you want to use data other actions have created (For example, to obtain a Zendesk ticket ID), select After other actions.
Writing Hooks
Click on a tab below to learn how to use this action for Filters and Actions.
Filter
WS Form PRO will call the filter as follows:
apply_filters($tag_name, $form, $submit);
$form– This contains the form object.$submit– This contains the submit object.
Return Values
The return from your PHP filter function will be processed as follows:
| Return Type | Processed As |
|---|---|
Object: $submit |
$submit object will be replaced and passed to the next action. |
| Integer: 0 – 100 | Spam level set 0 = Not spam, 100 = Spam. |
| Array | Return messages, field value setting or redirect to a different URL (See below). |
| Boolean: false | Action processing halted. |
Sample Code
Action
WS Form PRO will call the action as follows:
do_action($tag_name, $form, $submit);
- $form – This contains the form data.
- $submit – This contains the submit data.
Sample Code
Retrieve Field Data
The following code is an example of how to retrieve submitted field data from a form:
// Add action to process form data
add_action('wsf_action_tag', 'wsf_action_function', 10, 2);
// My function for action
function wsf_action_function($form, $submit) {
// Get submit value (Change '123' to your field ID)
$submit_value = wsf_submit_get_value($submit, 'field_123');
// Do something with $submit_value here
// ...
}
Using Conditional Logic to Catch Submit and Save Errors
An example of how to catch a form submission error is shown below. Similar conditions exist for a form save error too. In the example below a previously hidden message field is made visible if a submit error occurs.
