Drop-in Embed for Static HTML
The drop-in embed puts a Core Forms form on any HTML page with one element and one script. It needs no build step, framework or backend.
It suits a simple contact, signup or feedback form. The API key is visible in the page source, so turn on a captcha.
Add the Form
Finish the WordPress setup, then copy the snippet from the form’s Headless tab. A complete page looks like this:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Contact us</title>
</head>
<body>
<h1>Contact us</h1>
<div data-cf-form="contact" data-cf-key="YOUR_SITE_API_KEY"></div>
<script src="https://cms.example.com/wp-json/core-forms/v1/embed.js" defer></script>
</body>
</html>
Replace these 3 values:
data-cf-form: the form’s slug or numeric ID.data-cf-key: the site API key.- The script URL: your WordPress site.
You can put several hosts on one page and load the script once.
What the Script Does
- It finds every
[data-cf-form]element when the document is ready. - It fetches the form and places the HTML inside the element.
- It posts answers with the
X-Core-Forms-Keyheader, as JSON or as multipart when the visitor chose a file. - It adds
_cf_page_url, so referrer variables point at the visitor’s page. - It disables the button while sending. Then it shows the success message, follows the redirect URL or shows an error the visitor can retry.
The front end’s origin must be in Allowed origins when that list is filled in.
Events
Events fire on the host element and bubble up to the document.
| Event | When it fires | event.detail |
|---|---|---|
cf:mounted |
The form is on the page | The form schema |
cf:submit |
The visitor submits | { form }, the form element |
cf:success |
The entry is accepted, before any redirect | The response body |
cf:error |
Loading, the captcha or submitting fails | The error, response or response body |
document.addEventListener('cf:success', (event) => {
console.log('Submitted', event.detail);
});
For a payment form, cf:success means the checkout was created. The payment isn’t complete yet, and event.detail has a payment key.
Style the Form
The embed adds HTML without the WordPress stylesheets, so the form uses your site’s CSS. This is a small starting point:
.cf-form label,
.cf-form legend {
display: block;
margin-bottom: 0.375rem;
}
.cf-form input:not([type="checkbox"], [type="radio"]),
.cf-form select,
.cf-form textarea {
box-sizing: border-box;
width: 100%;
padding: 0.625rem;
font: inherit;
}
.cf-form [aria-invalid="true"] {
outline: 2px solid #b42318;
}
/* Used by generated legends and help text. */
.cf-form .screen-reader-text {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
Forms That Suit the Embed
The embed runs these by itself:
- Standard fields and file uploads.
- Next and Previous in multi-step forms, with a check on each step.
- Turnstile and reCAPTCHA v3.
- Payment forms with hosted checkout.
Conditional display, live calculations and save and resume use the WordPress scripts, so link to a standalone form page for those. Stripe’s inline payment flow also belongs on a WordPress page.
If your Content Security Policy limits scripts, allow challenges.cloudflare.com for Turnstile, or www.google.com and www.gstatic.com for reCAPTCHA. Fields and Features has the full table.
Load It in an App
When the host element appears after page load, call window.CoreFormsEmbed.mount(element). To mount every host that hasn’t been mounted yet, call window.CoreFormsEmbed.init().
React, Vue and Svelte has ready components.