Skip to main content

Command Palette

Search for a command to run...

CF7 Conditional Logic + API: Send Different Data Based on Field Value

Updated
•7 min read•View as Markdown
R
Experienced software developer skilled in solving real-world business and technical challenges through WordPress, custom web development, API integrations, and scalable software solutions. Passionate about building efficient, user-friendly digital products.

You have built a Contact Form 7 form with conditional logic. When a user selects "Support" from a dropdown, additional fields appear asking for their order number. When they select "Sales," different fields appear asking about their company size.

But when the form submits, all this data regardless of which fields were visible gets sent to your API. The CRM receives empty fields for the hidden questions, and your team has to manually sort through irrelevant data.

This is where conditional API logic comes in. Instead of sending everything, you can configure your integration to send only the data that matters, based on what the user actually selected.

The Problem with Sending Everything

Contact Form 7's conditional fields plugin hides fields visually, but it does not remove them from the form submission. This means every field is always submitted, even if the user never saw it.

For a CRM integration, this creates several problems:

  • Empty fields clutter your CRM. A lead record shows "Order Number: (empty)" and "Company Size: (empty)" alongside the fields that actually matter.

  • Automation triggers incorrectly. If your CRM workflow is based on field values, empty fields can cause the wrong automation to fire.

  • Data quality suffers. Your team cannot distinguish between "the user skipped this field" and "this field was not applicable."

How Conditional API Logic Solves This

The solution is to configure your API integration so that different conditions produce different payloads.

The "Contact Form to Any API" plugin supports conditional field logic in its API configuration. This means you can define rules like:

  • If inquiry-type equals "Support", send order-number and issue-description to the API.

  • If inquiry-type equals "Sales", send company-size and budget-range instead.

  • If budget-range is greater than 10000, send the lead to a different API endpoint entirely.

The result: your CRM receives clean, relevant data for every submission.

Setting Up Conditional Logic: The Two Layers

Conditional logic in a CF7-to-API integration involves two distinct layers.

Layer 1: CF7 Form Conditional Fields

First, you need conditional fields in your form itself. This is handled by a plugin like Conditional Fields for Contact Form 7.

This plugin lets you wrap form fields in [group] tags. Fields inside a group are hidden by default and only appear when a condition is met.

Example:

[select* inquiry-type "Support" "Sales" "Billing"]

[group support-fields]
[text* order-number placeholder "Order Number"]
[textarea* issue-description placeholder "Describe the issue"]
[/group]

[group sales-fields]
[text company-size placeholder "Company Size"]
[text budget-range placeholder "Budget Range"]
[/group]

You then configure conditions in the Conditional Fields tab: support-fields shows when inquiry-type equals "Support", and sales-fields shows when inquiry-type equals "Sales".

Layer 2: API Conditional Logic

This is where the API plugin's conditional feature comes in. In the "Contact Form to Any API" plugin settings, you configure which fields to send based on the same conditions.

The plugin's version 5.1.1 changelog confirms: "New - Added conditional field logic functionality in API configuration".

You define a condition in the API settings: "Only include order-number in the payload when inquiry-type equals 'Support'."

The plugin then evaluates this condition at submission time and builds the JSON payload accordingly.

A Practical Example: Routing Leads by Inquiry Type

Imagine you run a service business with two distinct offerings: residential and commercial. You want residential leads to go to your standard CRM, and commercial leads to go to a separate enterprise CRM.

Your CF7 form:

[select* customer-type "Residential" "Commercial"]

[group residential-fields]
[text* home-address placeholder "Home Address"]
[select* service-type "Cleaning" "Repair" "Installation"]
[/group]

[group commercial-fields]
[text* company-name placeholder "Company Name"]
[text* employee-count placeholder "Number of Employees"]
[select* contract-type "Monthly" "Annual" "Project-Based"]
[/group]

Your API integration setup:

Endpoint 1 (Residential CRM):

  • Condition: customer-type equals "Residential"

  • Payload: name, email, phone, home-address, service-type

Endpoint 2 (Commercial CRM):

  • Condition: customer-type equals "Commercial"

  • Payload: name, email, phone, company-name, employee-count, contract-type

With this configuration, a residential lead triggers a clean API call with only residential data. A commercial lead triggers a separate call with only commercial data. The CRMs never receive irrelevant empty fields.

Common Mistakes with Conditional API Logic

Using the same field name in multiple groups.

All field names in a CF7 form must be unique, even if they are in different conditional groups. If you have two groups both containing a field named your-email, the last group's value will overwrite the others, and your API will receive the wrong data.

Correct approach: Use unique names like residential-email and commercial-email, or map a single shared field outside the groups.

Forgetting that hidden fields are still submitted.

CF7's conditional fields plugin hides fields but does not remove them from the form submission. This means your API plugin will see all fields, including the hidden ones. You must configure the API plugin's conditional logic to exclude hidden fields from the payload.

Assuming the API plugin inherits CF7's conditional settings.

The "Contact Form to Any API" plugin has its own conditional logic settings. These are separate from the CF7 Conditional Fields plugin settings. You must configure both: one controls what the user sees, the other controls what the API receives.

Debugging Conditional API Issues

If your conditional API logic is not working, check these points:

Symptom Likely Cause Fix
All fields sent regardless of condition API conditional logic not configured Add conditions in the API plugin settings
Wrong field values sent Duplicate field names across groups Rename fields to be unique
Condition never triggers Field name mismatch Verify the exact field name used in the condition
Empty payload sent Condition syntax error Check Equals / Not Equals syntax in plugin settings

For a deeper dive into JSON structure mapping and conditional payload construction, read this guide on Contact Form 7 JSON mapping.

Beyond Basic Conditions: Advanced Routing

Once you have basic conditional logic working, you can build more sophisticated routing rules:

Multi-condition rules: Send a lead to Endpoint A only when customer-type equals "Commercial" AND employee-count is greater than 500.

Nested conditions: If service-type equals "Installation", include a site-survey-required field. If service-type equals "Repair", include a warranty-number field instead.

Fallback payloads: Define a default payload that sends when no other condition matches. This prevents empty API calls when the user's input does not fit any predefined pattern.

The plugin's conditional field support uses Equals and Not Equals operators, which covers the majority of routing scenarios. For more complex comparisons (greater than, less than), you may need the PRO version or a custom hook.

Key Takeaways

CF7 conditional fields hide fields but do not remove them from submission. You need API-level conditional logic to control what actually gets sent.

The "Contact Form to Any API" plugin supports conditional field logic. Version 5.1.1 added "conditional field logic functionality in API configuration".

Two layers of configuration are required. One in the CF7 form (to show/hide fields) and one in the API plugin (to include/exclude fields from the payload).

Field names must be unique across all conditional groups. Duplicate names cause data overwrites and incorrect API payloads.

Conditional API logic prevents empty fields from cluttering your CRM. This improves data quality and makes CRM automation more reliable.

With conditional logic configured at both the form and API layers, your integration sends exactly the data that matters for each submission no more, no less.