Run WordPress HookPRO

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:

  1. 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.
  2. Click the ‘Add’  icon to add a new action.
  3. 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.

If you want to manipulate field values before other actions run, select ‘Before’.

If you want to use the data after other actions have run (For example, to obtain a Zendesk ticket ID), select ‘After’.

Writing Hooks

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.

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.
Boolean: false Action processing halted.
Array: array(‘error’ => true)
  1. Action processing halted.
  2. Submit / Save error triggered (This can be caught with Conditional Logic).
Array: array(‘error’ => ‘Your error message’)
  1. Action processing halted.
  2. Submit / Save error triggered (This can be caught with Conditional Logic).
  3. Error message shown on form if Form Settings – ‘Show Server Side Error Messages’ setting is enabled.

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.

WS Form PRO - Run WordPress Hook Action - Catch Submit Errors

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
    // ...
}