# WordPress shortcode (no plugin)

## What this is

A single-file must-use plugin that registers `[contactusiq_form id="au_xxxxxxx"]`. The shortcode outputs a plain HTML form that posts straight from the visitor's browser to ContactUsIQ, so page caching, security plugins and `admin-ajax` are never involved. For sites that want the form without installing the full [ContactUsIQ plugin](../../apps/wp-plugin/).

Files: [`contactusiq-shortcode.php`](contactusiq-shortcode.php).

This sample uses the placeholder form id `au_xxxxxxx` and the Turnstile site key placeholder `0x4AAAAAAA_PLACEHOLDER`. Replace both in the shortcode attributes.

## Prerequisites

- WordPress 6.4+ on PHP 8.1+, with file access to `wp-content/`.
- A ContactUsIQ workspace with the site's domain verified and a published form.

## Steps

1. Copy `contactusiq-shortcode.php` to `wp-content/mu-plugins/contactusiq-shortcode.php` (create the `mu-plugins` folder if it does not exist). Must-use plugins load automatically; nothing to activate. It also works as a normal plugin in `wp-content/plugins/contactusiq-shortcode/` if you prefer to activate it.
2. Edit any page or post and add the shortcode:
   `[contactusiq_form id="au_xxxxxxx" sitekey="0x4AAAAAAA_PLACEHOLDER" redirect="https://www.example.com/thanks/" subject="Website enquiry"]`
3. Publish. The form appears in place of the shortcode.

Attributes: `id` (required), `sitekey` (Turnstile; omit to skip), `redirect` (absolute URL for the `303` after success; omit for the hosted thank-you), `subject`, `button` (label).

## The code

`add_shortcode('contactusiq_form', ...)` sanitises the attributes (`id` is restricted to `[A-Za-z0-9_-]`, `redirect` through `esc_url_raw`), enqueues the Turnstile script once when a `sitekey` is given, and echoes a form with `action="https://api.contactusiq.com/s/{id}"`, `method="POST"`, fields `name`, `email`, `message`, an off-screen empty `_honeypot`, hidden `_redirect`, `_subject` and `_page` (the current permalink), the `div.cf-turnstile`, and a submit button. All output is escaped with `esc_url`, `esc_attr` and `esc_html`. There is no nonce because nothing posts to WordPress.

## How to test

1. View the page on the live domain, fill the form, complete Turnstile, click Send.
2. Expect a `303` redirect to the `redirect` URL (or `200` with the hosted thank-you page if `redirect` is omitted).
3. The enquiry appears in the ContactUsIQ inbox with the page URL from `_page`.

## Troubleshooting

- **The shortcode text shows on the page.** The file is not in `wp-content/mu-plugins/` (check the exact path) or the shortcode name is misspelled. Logged-in editors see a hint if `id` is missing.
- **Redirect works but nothing in the inbox.** Origin or Referer mismatch: the site's domain is not verified on the form, or you are testing on a staging hostname. Verify the domain (the meta tag method suits WordPress well) or test on production.
- **Turnstile does not render.** Wrong `sitekey`, the domain is not on the Turnstile widget in Cloudflare, or another plugin already loads Turnstile with a different handle. In the last case remove `sitekey` and let that plugin render it, or dequeue theirs.
- **429.** 5 per IP per form per 10 minutes; wait for `Retry-After`.
- **Theme styles.** The form uses plain elements with class `contactusiq-form`; add CSS in your theme's customiser.

## For AI agents

To add a ContactUsIQ form to a WordPress site without the full plugin, copy `contactusiq-shortcode.php` to `wp-content/mu-plugins/` and insert `[contactusiq_form id="au_xxxxxxx" sitekey="0x4AAAAAAA_PLACEHOLDER" redirect="https://www.example.com/thanks/"]` into a page. The shortcode renders a plain HTML form whose `action` is `https://api.contactusiq.com/s/{id}` with an empty `_honeypot`, hidden `_redirect`, `_subject`, `_page` and the Turnstile widget. Success is a `303` to the redirect URL or `200`; errors are `400`, `404`, `413` and `429` with `Retry-After`.

```text
[contactusiq_form id="au_xxxxxxx" sitekey="0x4AAAAAAA_PLACEHOLDER" redirect="https://www.example.com/thanks/" subject="Website enquiry"]
```
