Wordpress/Contact Form 7 post request to Java Servlet
This article is an upgraded version of Joomla/RSForms post request to Java Servlet.
The use case is the same: Calling a Java Servlet, after submitting a form. The servlet should read the form data and start e.g. the creation of a license file.
The form plugin Contact Form 7 is very popular for Wordpress. It's powerful and easy to use.
But sadly, it doesn't support custom php scripts, only custom Javascript calls. So it's possible to add custom javascript calls with predefined hooks. Not exactly what we want because Javascript functions run on client side and not in the same context as the php backend.
It wasn't possible to implement missing features without coding php, but it was not tricky.
What we did to call a Java Servlet?
1. Register a custom module
Modified wp-content/plugins/contact-form-7/settings.php
...
self::load_module( 'submit' );
self::load_module( 'text' );
self::load_module( 'textarea' );
self::load_module( 'hidden' );
self::load_module( 'sibvisions' );
}
Added load_module('sibvisions');
2. Create the new module
Created wp-content/plugins/contact-form-7/modules/sibvisions.php
/**
** Module for SIB Visions.
**/
add_action('wpcf7_submit', 'wpcf7_sibvisions_submit', 10, 2);
function wpcf7_sibvisions_submit($contactform, $result)
{
if ($contactform->in_demo_mode() || $contactform->is_true('do_not_store'))
{
return;
}
$servletURL = $contactform->additional_setting('sib_servletURL');
if (empty($servletURL[0]))
{
error_log('Servlet URL is not set in form!');
return;
}
$cases = (array)apply_filters('wpcf7_sibvisions_submit_if',
array('spam', 'mail_sent', 'mail_failed'));
if (empty($result['status']) || ! in_array($result['status'], $cases ))
{
return;
}
$submission = WPCF7_Submission::get_instance();
if (!$submission || ! $posted_data = $submission->get_posted_data())
{
return;
}
if (isset($posted_data['g-recaptcha-response']))
{
if (empty($posted_data['g-recaptcha-response']))
{
return;
}
}
$fields_senseless = $contactform->scan_form_tags(
array('feature' => 'do-not-store'));
$exclude_names = array();
foreach ( $fields_senseless as $tag )
{
$exclude_names[] = $tag['name'];
}
$exclude_names[] = 'g-recaptcha-response';
foreach ($posted_data as $key => $value)
{
if ('_' == substr($key, 0, 1) || in_array($key, $exclude_names))
{
unset($posted_data[$key]);
}
}
$url = str_replace('"', "", $servletURL[0]);
$url = str_replace("'", "", $url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = array();
foreach ($posted_data as $post => $value)
{
if (is_array($value))
{
foreach ($value as $post2 => $value2)
{
$data[] = $post.'[]='.urlencode($value2);
}
}
else
{
$data[] = $post.'='.urlencode($value);
}
}
curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $data));
$data = curl_exec($ch);
if (curl_errno($ch))
{
wp_mail('noreply@sibvisions.com', 'Service error',
'Call to service ('.$url.') failed with error ('.curl_error($ch).')');
}
else
{
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)
if ($http_code != 200)
{
wp_mail('noreply@sibvisions.com', 'Service error',
'Call to service ('.$url.') failed with error ('.$data.')');
}
}
curl_close($ch);
}
The script is based on module flamingo.php because it had all useful validations.
Be careful, because the script will be applied to all your forms. It's form independent.
3. Additional setting
The only thing you'll need is am additional setting (for your form):
This setting configures the servlet to use for the form. If you don't configure a servlet, the module will do nothing!
DONE
The new module will forward all form data to the servlet. The servlet is the same as in our original article - no changes needed.