PRO

Run WordPress Hook Action

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

Return a Submit Object

You can use the Run WordPress Hook action in combination with some simple code to intercept submission data and use it in your own PHP scripts. By using a WordPress filter you can return the submit object back to WS Form for use in other actions.

An example of how to do this is shown below:

// Add filter
add_filter('wsf_filter_tag', 'wsf_filter_function', 10, 2);

// Filter function
function wsf_filter_function($form, $submit) {

    // Set meta key for field ID 123 (Change this to your field ID)
    $meta_key = 'field_123';

    // Get value submit object
    $field_value_old = $submit->meta[$meta_key]['value'];

    // Do something with $field_value_old (This is just an example)
    $field_value_new = str_replace('replace_this', 'with_this', $field_value_old);

    // Set value in submit object
    $submit->meta[$meta_key]['value'] = $field_value_new;

    // Return the submit object back to WS Form
    return $submit;
}

When the filter runs, the form and submit data are passed to it.

In the example above, you would change field_123 to match the field ID on your form. For example, if the field you want to obtain has an ID of 321 in the WS Form layout editor, you would enter field_321 as the $meta_key variable.

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