以前、「Contact Form 7で選択肢によってメールの送信先を変更する方法」という記事を書きました。”AAAについて|aaa@example.com”といったように、|で選択肢とメールアドレスを分けてフォームタグ内に記述することで、簡単にメールの送信先を振り分けることができます。
ただ、Contact Form 7 Multi-Step Formでフォームを分割していると、この方法は使えません。
そこで今回は、フォームを分割していてもメールの送信先を振り分けられるように、アクションフックを使って送信先を変更する方法をご紹介いたします。
アクションフックを使ってフォームの送信先メールアドレスを変更する方法
まずフォーム側では、以下のように通常通りセレクトボックスを設置します。
[select* contact-select "選択肢1" "選択肢2" "選択肢3"]
適用しているテーマのfunctions.phpや自作プラグイン、スニペット系のプラグインで以下のようなPHPコードを追加します。
“contact-select”となっている箇所はフォームタグの名前なので変更してください。また、”case ‘選択肢1′”などの選択肢の値や送信先メールアドレスも環境に合わせて適宜変更してください。
メールの送信先を上書きする場合
add_action('wpcf7_before_send_mail', function ($contact_form, &$abort, $submission) {
$posted_data = $submission->get_posted_data();
if (!isset($posted_data['contact-select'])) {
return;
}
$contact_select_value = $posted_data['contact-select'];
if (is_array($contact_select_value)) {
$contact_select = $contact_select_value[0];
} else {
$contact_select = $contact_select_value;
}
$mail = $contact_form->prop('mail');
$contact_select_email = '';
switch ($contact_select) {
case '選択肢1':
$contact_select_email = 'select01@example.com';
break;
case '選択肢2':
$contact_select_email = 'select02@example.com';
break;
case '選択肢3':
$contact_select_email = 'select03@example.com';
break;
}
if (!empty($contact_select_email)) {
$mail['recipient'] = $contact_select_email;
$contact_form->set_properties(['mail' => $mail]);
}
}, 10, 3);
メールタブで設定している送信先にも同時に送信する場合
add_action('wpcf7_before_send_mail', function ($contact_form, &$abort, $submission) {
$posted_data = $submission->get_posted_data();
if (!isset($posted_data['contact-select'])) {
return;
}
$contact_select_value = $posted_data['contact-select'];
if (is_array($contact_select_value)) {
$contact_select = $contact_select_value[0];
} else {
$contact_select = $contact_select_value;
}
$mail = $contact_form->prop('mail');
$contact_select_email = '';
switch ($contact_select) {
case '選択肢1':
$contact_select_email = 'select01@example.com';
break;
case '選択肢2':
$contact_select_email = 'select02@example.com';
break;
case '選択肢3':
$contact_select_email = 'select03@example.com';
break;
}
if (!empty($contact_select_email)) {
$original_recipient = $mail['recipient'];
$mail['recipient'] = $original_recipient . ',' . $contact_select_email;
$contact_form->set_properties(['mail' => $mail]);
}
}, 10, 3);
メールタブで設定している送信先をCCに入れる場合
add_action('wpcf7_before_send_mail', function ($contact_form, &$abort, $submission) {
$posted_data = $submission->get_posted_data();
if (!isset($posted_data['contact-select'])) {
return;
}
$contact_select_value = $posted_data['contact-select'];
if (is_array($contact_select_value)) {
$contact_select = $contact_select_value[0];
} else {
$contact_select = $contact_select_value;
}
$mail = $contact_form->prop('mail');
$contact_select_email = '';
switch ($contact_select) {
case '選択肢1':
$contact_select_email = 'select01@example.com';
break;
case '選択肢2':
$contact_select_email = 'select02@example.com';
break;
case '選択肢3':
$contact_select_email = 'select03@example.com';
break;
}
if (!empty($contact_select_email)) {
$original_recipient = $mail['recipient'];
$mail['recipient'] = $contact_select_email;
$mail['cc'] = $original_recipient;
$contact_form->set_properties(['mail' => $mail]);
}
}, 10, 3);
あとがき
PHPをいじらない方法の方が簡単ですが、フックを使えば柔軟に対応できますね。
選択肢によってメールの送信先を変更したい時は、ぜひ参考にしていただければ幸いです。



コメントを残す