BlogIntegrations · November 6, 2026 · 5 min read

Contact Form 7 Text Escaping: Preserve Ampersands and Literal Markup Across Systems

Trace CF7 text through JSON and HTML boundaries. Diagnose double encoding, preserve literal entities and render submitted text in the intended context.

A Contact Form 7 inquiry says Research & Development, but a custom lead viewer displays Research & Development. Another message contains literal angle brackets that disappear after a connector tries to clean them. Both symptoms call for tracing text representations across boundaries, rather than applying another replacement to the final screen.

Two ampersand cards connected through transparent frames beside a magnifying glass.
Generated conceptual illustration; not a product screenshot or measured result.

This guide concerns adapters and viewers you maintain. Different CF7 extensions serialize fields differently, so inspect their actual output. The examples are synthetic text-preservation cases, not a claim about a defect in CF7 or a ready-made integration supplied by CF7 Analytics.

Distinguish the value from its representation

An ampersand in a person’s answer is a character in a string. An HTML entity is one way to represent that character inside HTML source. JSON uses its own string serialization rules. Seeing an escape sequence in a network response is not automatically evidence that the stored value contains those literal characters.

Inspect the parsed value as well as the source. JSON.stringify() produces JSON text; parsing that JSON returns data values. Do not pre-escape every ampersand for HTML merely because the JSON will eventually reach a browser. The output destination determines the needed treatment.

Start with a boundary map: submitted field, connector’s parsed string, serialized request, destination’s parsed string, stored field, and rendered text. At each boundary record the expected representation. This turns an ambiguous report of “bad characters” into a precise statement about where a value first changed.

Build a small fixture set

Use fictional values that expose different errors: an ordinary ampersand, quotation marks, literal markup-like text, non-English characters, and a string containing an entity name that the visitor genuinely typed. A literal entity-looking answer is especially useful because automatic decoding can silently change its meaning.

Research & Development
Use <sample> as the example label
The literal sequence is &amp;
She wrote "hello" to Zoë

The displayed third line intentionally contains an ampersand followed by amp and a semicolon. Preserve that complete literal sequence when that is what the visitor supplied. A rule that repeatedly decodes until no entities remain will destroy this distinction and can turn ordinary text into markup-looking content.

Attach expected values to the fixtures at each boundary. Compare characters and lengths in a controlled environment rather than judging only a browser screenshot. Never use real inquiry contents as public debugging examples, and avoid logging entire payloads when a short synthetic reference is sufficient.

Keep JSON serialization separate from HTML output

Build an object or array from the intended data values and use the platform’s JSON serializer. In WordPress, wp_json_encode() encodes data with additional confidence checks and can return false. Handle a serialization failure before attempting the downstream request.

Do not assemble JSON by concatenating submitted strings or by replacing quotation marks manually. Those approaches mix data construction with serialization and make unusual answers hard to reason about. Confirm the destination parses the request once and receives the expected string, rather than a JSON document embedded as another string.

A serializer may choose a valid escaped representation without changing the parsed value. Record both forms in the fixture evidence so a future reviewer does not “repair” a harmless wire representation. The existing URL-encoded payload guide addresses a different transport format with different character rules.

Render ordinary text in the intended context

For a custom browser viewer that should show plain text, textContent sets text without interpreting it as HTML. A stored value containing angle brackets should remain visible text when that is the viewer’s contract. Avoid moving it into an HTML parsing operation just to preserve line breaks.

For server-rendered WordPress output, use the escaping function appropriate to the destination. The WordPress escaping handbook separates HTML text, attributes, URLs and other contexts. Escape close to output so the storage and transport layers can retain the intended data value.

There is an important WordPress-specific nuance: esc_html() avoids double-encoding recognized existing entities. Do not assume it always treats a literal entity-looking sequence exactly like a browser text node would. If exact literal preservation is a requirement, evaluate that fixture in the chosen renderer and use its documented policy consistently.

Repair the first incorrect boundary

If the connector stores HTML-escaped text as its canonical string, fix that contract through a reviewed migration rather than adding a universal decoder in every viewer. Existing records may contain a mixture of raw and escaped text. You cannot reliably distinguish those populations by searching for an ampersand alone.

Python’s html module provides explicit escaping and unescaping functions. Their availability does not establish that a particular record needs decoding. Use provenance, known source versions and controlled examples before changing stored values. Preserve a scoped before-and-after record for any approved correction.

Verify the complete round trip

Run every fixture through the actual adapter and viewer, then copy the displayed text back into a comparison step. Check optional empty fields and repeated saves too. A viewer that writes its escaped display value back into storage can introduce another layer on every edit.

Keep ordinary text preservation distinct from permitting rich HTML. Our HTML sanitizer comparison covers the latter decision. This site publishes CF7 Analytics; use the official Svetlik Analytics for Contact Form 7 free plugin for form outcome trends, alongside these field-level checks. A successful submission count cannot certify that another system preserved every character.