Integration With Contact Form 7 For WordPress

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 NameFor
your-nameFor Customer Name
your-phoneCustomer Phone
your-emailCustomer Email
your-messageAll 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');
 */

How to add it in WordPress?

Here’s the quickest way to use that ZNICRM script with Contact Form 7 on WordPress—end-to-end.

1) Prep in ZNICRM

  • Generate an API key in ZNICRM. You’ll paste this into WordPress in a moment. 

2) Map your CF7 fields

In your Contact Form 7 form, make sure (or rename) fields to these keys:

  • your-name → customer name
  • your-phone → customer phone
  • your-email → customer email
  • your-message → everything else / message

ZNICRM requires either phone or email to be present. 

3) Create a tiny integration plugin (recommended)

This keeps things update-proof and tidy.

  1. In your WordPress install, create: wp-content/plugins/cf7-znicrm/cf7-znicrm.php
  2. Paste the code below (it’s a hardened, production-friendly version of the guide’s snippet with safe constants and logs):
<?php
/**
 * Plugin Name: Contact Form 7 → ZNICRM (API Sender)
 * Description: Creates a ZNICRM lead from a Contact Form 7 submission.
 * Version: 0.2.1
 * Author: You
 */

if (!defined('ABSPATH')) exit;

if (!function_exists('cf7_znicrm_log')) {
  function cf7_znicrm_log($msg) {
    if (defined('WP_DEBUG') && WP_DEBUG) {
      error_log('[CF7→ZNICRM] ' . (is_scalar($msg) ? $msg : print_r($msg, true)));
    }
  }
}

/**
 * Add these (preferably) in wp-config.php:
 *   define('ZNICRM_API_KEY', 'YOUR_API_KEY');
 *   define('ZNICRM_DEPT_ID', 0); // Optional department integer
 */

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)) {
    cf7_znicrm_log('Missing API key; skipping.');
    return;
  }

  if (!$submission && class_exists('WPCF7_Submission')) {
    $submission = WPCF7_Submission::get_instance();
  }
  if (!$submission) {
    cf7_znicrm_log('No submission instance; skipping.');
    return;
  }

  $posted  = $submission->get_posted_data();

  // Map CF7 fields (update if your form uses different names)
  $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']): '';

  // ZNICRM accepts data via GET per their guide
  $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())
  );

  $url = 'https://api.intueri.io/weblead.php?' . http_build_query($payload);

  $response = wp_remote_get($url, array(
    'timeout'   => 10,
    'sslverify' => true,
    'headers'   => array('Accept' => 'application/json'),
  ));

  if (is_wp_error($response)) {
    cf7_znicrm_log('Request error: ' . $response->get_error_message());
    // If you hook BEFORE send, you may cancel email on CRM failure by setting $abort = true.
    if (is_bool($abort)) {
      $abort = true;
      $submission->set_response(__('We could not process your request right now. Please try again later.', 'cf7-znicrm'));
    }
    return;
  }

  cf7_znicrm_log('Pushed to ZNICRM (HTTP ' . wp_remote_retrieve_response_code($response) . ').');
}

/** 
 * Pick ONE of these hooks:
 * 1) AFTER CF7 mail is sent (safer; won’t block user emails on CRM hiccups)
 */
add_action('wpcf7_mail_sent', 'cf7_znicrm_send', 10, 1);

/**
 * 2) OR BEFORE sending mail; optionally cancel CF7 email if CRM push fails
 * add_action('wpcf7_before_send_mail', 'cf7_znicrm_send', 10, 3);
 * To always skip CF7’s email entirely:
 * add_filter('wpcf7_skip_mail', '__return_true');
 */

This mirrors the official guide’s logic (same expected CF7 fields and the weblead.php endpoint) while keeping secrets out of code and adding better error handling. 

  1. Activate Contact Form 7 → ZNICRM (API Sender) in Plugins → Installed Plugins.

4) Put your API key in 

wp-config.php

Add (above the line “That’s all, stop editing!”):

define('ZNICRM_API_KEY', 'PASTE_YOUR_ZNICRM_API_KEY_HERE');
define('ZNICRM_DEPT_ID', 0); // optional, per your ZNICRM setup

The guide instructs you to set the API key and (optionally) a department ID for the request. Using wp-config.php keeps it out of version control and the DB. 

5) Test it

  • Submit your CF7 form with either email or phone present.
  • In WP_DEBUG mode, check your PHP error log for lines starting with [CF7→ZNICRM] to confirm the push. The guide notes that at least one of phone/email must be there for success. 

FAQ / Tweaks

  • My form uses different field names. Update the $posted[…] keys in the plugin to match your CF7 form tags (e.g., change your-message to message). The expected names are listed in the guide. 
  • I only want to create the lead and not send CF7 email. Uncomment the wpcf7_before_send_mail hook and add add_filter(‘wpcf7_skip_mail’, ‘__return_true’); (shown in the code). The guide mentions both after-send and before-send options. 
  • Where does the data go? It’s sent via GET to https://api.intueri.io/weblead.php with apikey, leadName, leadEmail, leadPhone, department, and a combined leadQuery built from subject/message/form title—per guide. 

If you want, drop your current CF7 form markup here and I’ll tailor the mapping section exactly to your field names.