Contact Forms
Build forms with TOFU. The form is defined in a settings file, and its input, confirm and result screens are ordinary page templates, so the markup is written like any other page. The contact form (/contact/) is the sample.
Adding a form takes four steps. The example below adds a recruitment form at /recruit/entry/.
1. Add the pages
Add the input, confirm and result pages to settings/pages.php. Keep the confirm and result pages out of search engines.
// settings/pages.php
'recruit/entry' => ['title' => '採用応募'],
'recruit/entry/confirm' => ['title' => '入力内容の確認', 'seo' => ['noindex' => true]],
'recruit/entry/result' => ['title' => '送信完了', 'seo' => ['noindex' => true]],
2. Define the form
Add one file per form to settings/forms/. It returns a FormConfig. The values shared by every form (sender, admin address, subject prefix) are in settings/forms/_common.php and are available as $common.
// settings/forms/recruit.php
return new FormConfig(
key: 'recruit',
name: '採用応募',
template: new TemplateConfig(
inputPath: '/recruit/entry/',
confirmPath: '/recruit/entry/confirm/',
resultPath: '/recruit/entry/result/',
),
mail: new MailConfig(
fromEmail: $common['fromEmail'],
fromName: $common['fromName'],
recipients: new MailRecipientsCollection([
new MailRecipientsConfig(
recipientEmail: $common['adminEmail'],
subject: $common['subjectPrefix'] . '採用応募がありました',
mailBodyPath: 'form/recruit/admin-body',
),
]),
),
validation: new ValidationConfig(
allows: ['name', 'email'],
rules: ['name' => 'required', 'email' => 'required|email'],
),
recaptchaEnabled: defined('TOFU_RECAPTCHA_SITE_KEY'),
confirmStep: true,
);
- Only the fields in
allowsare accepted. - Write the production addresses from the start. Mail sent from local and test environments is caught by Mailpit and never delivered, and its subject gets the "【TEST】" prefix.
- The reCAPTCHA keys go in
wp-config.php, not in the theme.
3. Write the templates
Copy page-contact.php, page-contact__confirm.php and page-contact__result.php, rename them after the new pages (page-recruit__entry.php and so on), and change $form_key.
// page-recruit__entry.php
<?php
use TofuPlugin\Helpers\Form;
// Show as a regular page when TOFU is inactive
if (!class_exists(Form::class)) {
require get_theme_file_path('page.php');
return;
}
$form_key = 'recruit';
$form_action = 'input';
// Load reCAPTCHA and other scripts before get_header()
Form::embedScript($form_key);
get_header();
?>
<?php echo Form::formOpen($form_key, $form_action, ['class' => 'c-form']); ?>
<input type="text" name="name" value="<?php echo Form::value($form_key, 'name'); ?>" required>
<?php get_template_part('form/error', null, ['key' => $form_key, 'field' => 'name']); ?>
<button type="submit" class="c-button">確認画面へ</button>
<?php echo Form::formClose($form_key, $form_action); ?>
<?php get_footer(); ?>
- The confirm page checks
Form::verifySession()and the result pageForm::verifySubmit(), and send visitors who skipped a step back to the input page. form/error.phpprints a field's error messages.
4. Write the mail bodies
Put the mail bodies in form/{form key}/, using form/contact/admin-body.php and form/contact/auto-reply-body.php as the model. Mail is plain text, so print the values with Form::value($form_key, 'name', true) (unescaped).
Testing
Go through input → confirm → result on the local site and check the mail in Mailpit. See the TOFU documentation for the validation rules and the other options.