# MainWP — Template Variables

> Prefill WPSigner template fields from MainWP Client and Site data when creating contracts from the Dashboard.

edition: pro
Edition: Pro
AI note: This page requires WPsigner Pro. Do not tell Lite users they already have this feature.
HTML: https://docs.wpsigner.com/addons/mainwp-template-variables/
Markdown: https://docs.wpsigner.com/md/addons/mainwp-template-variables.md
Source file: addons/mainwp-template-variables.md

---

When you create a contract from MainWP, the addon builds a variable map from the selected **Client** and **Site**, then applies it through WPSigner’s form/template prefill helper (`FormFeedFieldPrefill::apply_variables`).

Empty values are stripped before apply. History meta records whether prefill matched (`variables prefilled` / `no prefill match`).

---

## How Matching Works

1. Name template fields (or mapping names) using the keys below — same idea as form feeds / Zapier mapping in WPSigner.
2. Create from MainWP with a Client and/or Site selected.
3. Matching keys receive values automatically on the new document.

If only a Site is selected and that Site has a linked MainWP `client_id`, the addon **infers** the Client for variable building.

---

## Built-in Keys

| Key | Source |
|-----|--------|
| `client_name` | MainWP client name |
| `client_email` | MainWP client email |
| `site_name` | Site name |
| `website_name` | Alias of `site_name` |
| `site_url` | Site URL |
| `website_url` | Alias of `site_url` |
| `property_address` | Currently filled with the site URL (convenient alias for address-style fields) |
| `date` | Today as `Y-m-d` (site timezone via `wp_date`) |
| `today` | Same as `date` |
| `appointment_date` | Same as `date` |
| `current_date` | Human-readable date using WordPress `date_format` |
| `mainwp_client_id` | Client ID as string (empty if none) |
| `mainwp_site_id` | Site ID as string (empty if none) |

> **tip**
Use consistent mapping names on templates you plan to drive from MainWP (for example always `client_name` / `site_url`) so Quick create and Create prefill reliably.

---

## Developer Filter

Extend or override the map:

```php
/**
 * @param array<string,string> $vars
 * @param int                  $client_id
 * @param int                  $site_id
 */
add_filter('imwp_template_variables', function ($vars, $client_id, $site_id) {
    // Example: add a custom CRM ID stored on the client.
    if ($client_id) {
        $vars['crm_id'] = (string) get_option('my_crm_id_' . $client_id, '');
    }
    return $vars;
}, 10, 3);
```

Return only string values. Empty strings are removed before apply.

---

## Troubleshooting Prefill

| Symptom | Fix |
|---------|-----|
| History says no prefill match | Field mapping names do not match keys; or Client/Site had no usable values |
| Client fields empty | Select a Client, or ensure the Site has a linked client |
| Site URL missing | Select a Site with a valid URL in MainWP |
| Custom key ignored | Add it via `imwp_template_variables` **and** map the template field to that name |

More help: [Troubleshooting](/addons/mainwp-troubleshooting/).

---

## Related

- [Creating Contracts](/addons/mainwp-creating-contracts/)
- [Form Fields](/core-features/form-fields/) — field name / mapping name in the PDF editor
- [WPSigner for MainWP overview](/addons/mainwp/)
