Sending form data to another script

The Quform WordPress v2 documentation is not yet finished. Please post on the forums if you have any questions in the mean time.

Click here to visit the complete documentation for Quform 1.x

In this guide we will show you to how send the form data to another script, while maintaining the form validation and success actions of the Quform plugin.

Sending all form data

Add the following code to the theme functions.php file (or create a plugin for it.)

123
4
5
6
7
add_action('quform_post_process_1', function (array $result, Quform_Form $form) {    wp_remote_post('http://www.example.com/script.php', array(        'body' => $_POST
    ));
 
    return $result;
}, 10, 2);
add_action('quform_post_process_1', function (array $result, Quform_Form $form) {
    wp_remote_post('http://www.example.com/script.php', array(
        'body' => $_POST
    ));

    return $result;
}, 10, 2);
  • On line 1, replace the number 1 with the form ID
  • On line 2, change the URL to the other script URL
1
2
34
5
6
7
8
9
function my_post_process(array $result, Quform_Form $form)
{
    wp_remote_post('http://www.example.com/script.php', array(        'body' => $_POST
    ));
 
    return $result;
}
add_action('quform_post_process_1', 'my_post_process', 10, 2);
function my_post_process(array $result, Quform_Form $form)
{
    wp_remote_post('http://www.example.com/script.php', array(
        'body' => $_POST
    ));

    return $result;
}
add_action('quform_post_process_1', 'my_post_process', 10, 2);
  • On line 3, change the URL to the other script URL
  • On line 9, replace the number 1 with the form ID

Rewriting the variable names

If the other script is expecting the values in the post data to have different names, you can rewrite them before sending the data.

Add the following code to the theme functions.php file (or create a plugin for it.)

12
34567
8
910
11
12
13
14
add_action('quform_post_process_1', function (array $result, Quform_Form $form){    $data = array(
        'name' => $form->getValue('quform_1_1'),        'email' => $form->getValue('quform_1_2'),        'phone' => $form->getValue('quform_1_3'),        'message' => $form->getValue('quform_1_4')    );
 
    wp_remote_post('http://www.example.com/script.php', array(        'body' => $data
    ));
    
    return $result;
}, 10, 2);
add_action('quform_post_process_1', function (array $result, Quform_Form $form){
    $data = array(
        'name' => $form->getValue('quform_1_1'),
        'email' => $form->getValue('quform_1_2'),
        'phone' => $form->getValue('quform_1_3'),
        'message' => $form->getValue('quform_1_4')
    );

    wp_remote_post('http://www.example.com/script.php', array(
        'body' => $data
    ));
    
    return $result;
}, 10, 2);
  • On line 1, change the number 1 to the form ID
  • On lines 3 to 6, change the array so that the key is the name of the post variable the other script is expecting and replace 1_1, 1_2, 1_3 and 1_4 with the element unique ID of the elements to get the value from
  • On line 9, change the URL to the other script URL
1
2
3
45678
9
1011
12
13
14
15
16
function my_post_to_another_script(array $result, Quform_Form $form)
{
    $data = array(
        'name' => $form->getValue('quform_1_1'),        'email' => $form->getValue('quform_1_2'),        'phone' => $form->getValue('quform_1_3'),        'message' => $form->getValue('quform_1_4')    );
 
    wp_remote_post('http://www.example.com/script.php', array(        'body' => $data
    ));
    
    return $result;
}
add_action('quform_post_process_1', 'my_post_to_another_script', 10, 2);
function my_post_to_another_script(array $result, Quform_Form $form)
{
    $data = array(
        'name' => $form->getValue('quform_1_1'),
        'email' => $form->getValue('quform_1_2'),
        'phone' => $form->getValue('quform_1_3'),
        'message' => $form->getValue('quform_1_4')
    );

    wp_remote_post('http://www.example.com/script.php', array(
        'body' => $data
    ));
    
    return $result;
}
add_action('quform_post_process_1', 'my_post_to_another_script', 10, 2);
  • On lines 4 to 7, change the array so that the key is the name of the post variable the other script is expecting and replace 1_1, 1_2, 1_3 and 1_4 with the element unique ID of the elements to get the value from
  • On line 10, change the URL to the other script URL
  • On line 16, change the number 1 to the form ID

Troubleshooting

If the integration is not working you can log the response to the WordPress error log. First, save the response into a variable by changing this line:

1
wp_remote_post('http://www.example.com/script.php', array(
wp_remote_post('http://www.example.com/script.php', array(

To this:

1
$response = wp_remote_post('http://www.example.com/script.php', array(
$response = wp_remote_post('http://www.example.com/script.php', array(

Then before this line:

1
return $result;
return $result;

Add this debugging code:

1
2
3
4
5
6
7
8
9
10
if (defined('WP_DEBUG') && WP_DEBUG) {
    if (is_wp_error($response)) {
        Quform::log($response->get_error_message());
    } else {
        $code = wp_remote_retrieve_response_code($response);
        $body = wp_remote_retrieve_body($response);
 
        Quform::log('HTTP status code: ' . $code, $body);
    }
}
if (defined('WP_DEBUG') && WP_DEBUG) {
    if (is_wp_error($response)) {
        Quform::log($response->get_error_message());
    } else {
        $code = wp_remote_retrieve_response_code($response);
        $body = wp_remote_retrieve_body($response);

        Quform::log('HTTP status code: ' . $code, $body);
    }
}

Then temporarily enable debug logging then submit the form again, any errors will be logged to the wp-content/debug.log file. Once you’ve sorted the issue you can remove the debugging code and turn off debug logging.

Still having trouble? Head over to the forums.

Forums