I am doing some Wordpress (6.1.1) work for friends of mine, I am not very experienced in WP and PHP(7.4.26).
I need to perform an HTTP POST request to a 3rd party CRM system after a form has been submitted, this form is created by the WP plugin Super Forms 6.3.312. Super Forms has a global Form setting that lets you POST the form data on submit on a given URL (default behavior is emailing):
I have tried using the exact (windows) path to the PHP file, and the localhost path to the PHP file (through the WP server) but that does not seem to start the script. I already found out that you can make POST requests to PHP files, and that the $_POST variable will be filled with an array of all form data, so I know how to get the data once the PHP file has been started after the form submission.
My questions:
- What is the best approach to perform an HTTP POST request after a Super Forms form has been submitted?
- Where do I put my PHP script?
- What is the (relative) path to that PHP script in my Super Forms Forms Settings?
Examples:
- This is the script that should be executed upon form submission, the $_POST variable should be filled with the form data IMHO (simplified) :
$company = $_POST['company'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phonenumber = $_POST['phonenumber'];
$address = $_POST['address'];
$zipcode = $_POST['zipcode'];
$consumption = $_POST['consumption'];
$city = $_POST['city'];
$connectiontype = $_POST['connectiontype'];
$question = $_POST['question'];
$body = array(
'company' => $company,
'firstname' => $firstname,
'lastname' => $lastname,
'email' => $email,
'phonenumber' => $phonenumber,
'address' => $address,
'zipcode' => $zipcode,
'consumption' => $consumption,
'city' => $city,
'connectiontype' => $connectiontype,
'question' => $question,
);
$args = array(
'body' => $body
);
$response = wp_remote_post("URL of CRM system here", $args);
$body = wp_remote_retrieve_body($response);
CodePudding user response:
There are several ways of doing this. But a WordPress hook is always involved
First of all, WordPress has built-in hooks to use and check when a "post" with a specific post type was created. Every time Super Forms submits a form that has "Contact entries" enabled, it creates a new post with the post type "super_contact_entry". https://renstillmann.github.io/super-forms/#/data-storage?id=where-are-the-contact-entries-stored
Info about this WP hook here: https://developer.wordpress.org/reference/hooks/wp_insert_post/
There are also hooks that can listen to posts being updated, so you can compare the previous post status, or any metadata in that regard and do things.
Super Forms itself also has hooks, to listen to a form submission, details and example codes here: https://renstillmann.github.io/super-forms/#/hook-examples
The one that helped me was Send submitted form data to another site