You can download and upload this file in your plugin directory of wordpress.
Please create an API key in the ZNICRM to update in this plugin. Assign the newly created API Key with $znicrm_apikey parameter.
Also, ensure that your contact form have the following fields or update the correct fields in the plugin.
Field Name | For |
your-name | For Customer Name |
your-phone | Customer Phone |
your-email | Customer Email |
your-message | All other data should be clubbed into this field |
Either phone or email should be present in the form data for successful submission.
<?php
/**
* Plugin Name: Contact Form 7 → ZNICRM (API Sender)
* Description: Creates a ZNICRM lead from a Contact Form 7 submission.
* Version: 0.2.0
* Author: Intueri Inc.
* Text Domain: cf7-znicrm
*/
if (!function_exists('write_log')) {
function write_log($log) {
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log(is_scalar($log) ? $log : print_r($log, true));
}
}
}
/**
* Configure these safely (e.g., in wp-config.php):
* define('ZNICRM_API_KEY', '...'); define('ZNICRM_DEPT_ID', 0);
*/
function cf7_znicrm_send($contact_form, &$abort = null, $submission = null) {
$api_key = defined('ZNICRM_API_KEY') ? ZNICRM_API_KEY : '';
$dept_id = defined('ZNICRM_DEPT_ID') ? (int) ZNICRM_DEPT_ID : 0;
if (empty($api_key)) {
write_log('ZNICRM: Missing API key');
return;
}
// Older CF7 passes $submission only on before_send; on mail_sent we must fetch it.
if (!$submission && class_exists('WPCF7_Submission')) {
$submission = WPCF7_Submission::get_instance();
}
if (!$submission) {
write_log('ZNICRM: No submission instance available');
return;
}
$posted = $submission->get_posted_data();
$name = isset($posted['your-name']) ? sanitize_text_field($posted['your-name']) : '';
$email = isset($posted['your-email']) ? sanitize_email($posted['your-email']) : '';
$phone = isset($posted['your-phone']) ? sanitize_text_field($posted['your-phone']) : '';
$subject = isset($posted['your-subject']) ? sanitize_text_field($posted['your-subject']) : '';
$message = isset($posted['your-message']) ? sanitize_textarea_field($posted['your-message']) : '';
$payload = array(
'apikey' => $api_key,
'leadName' => $name,
'leadEmail' => $email,
'leadPhone' => $phone,
'department' => $dept_id,
'leadQuery' => sprintf('%s #*# Message: %s #*# Form Title: %s',
$subject,
$message,
$contact_form->title())
);
$query = http_build_query($payload);
$url = 'https://api.intueri.io/weblead.php?' . $query;
$response = wp_remote_get($url, [
'timeout' => 10,
'sslverify' => true,
'headers' => ['Accept' => 'application/json'],
]);
if (is_wp_error($response)) {
write_log('ZNICRM: Request error -> ' . $response->get_error_message());
// If you want to *stop* CF7 email when CRM fails *and* you're on before_send:
if (is_bool($abort)) {
$abort = true;
$submission->set_response('We could not process your request right now. Please try again later.');
}
return;
}
$code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
write_log('ZNICRM: HTTP ' . $code);
// Avoid logging secrets; log only minimal info
write_log('ZNICRM: Body len ' . strlen($body));
// Optional: parse/validate success from API and decide to abort email on failure (before_send only)
// $data = json_decode($body, true); if (!$data || empty($data['ok'])) { ... }
}
/**
* Pick ONE of these hooks:
* 1) Run AFTER CF7 successfully sends mail:
*/
add_action('wpcf7_mail_sent', 'cf7_znicrm_send', 10, 1);
/**
* 2) Or run BEFORE and optionally skip CF7 mail if CRM fails:
* add_action('wpcf7_before_send_mail', 'cf7_znicrm_send', 10, 3);
* To always skip CF7 mail (i.e., you want only the CRM and no email), use:
* add_filter('wpcf7_skip_mail', '__return_true');
*/