動作確認:Contact Form 7 Version 4.2.2
「Contact Form 7」でメール送信前に何かの処理を行うには「wpcf7_before_send_mail」フックを利用します。
[参考]
Issue with wpcf7_before_send_mail
CONTACT FORM 7 3.9 ベータ
functions.php に wpcf7_before_send_mail フックを追加
add_filter('wpcf7_before_send_mail', 'my_wpcf7_before_send_mail', 1, 1);
function my_wpcf7_before_send_mail($cf7)
{
// ここにいろいろ処理を書く
// code...
return $cf7;
}
送信先メールアドレスの変更
選択項目によってメールアドレスを切り替えたい場合等に使える。
add_filter('wpcf7_before_send_mail', 'my_wpcf7_before_send_mail', 1, 1);
function my_wpcf7_before_send_mail($cf7)
{
// メール送信に関する情報を取得
$mail = $cf7->prop('mail');
// メールアドレスの上書き
$mail['recipient'] = 'sample@yahoo.co.jp';
// 情報を再セット
$cf7->set_properties( array( 'mail' => $mail ));
return $cf7;
}
ユーザーが入力した内容を取得する
add_filter('wpcf7_before_send_mail', 'my_wpcf7_before_send_mail', 1, 1);
function my_wpcf7_before_send_mail($cf7)
{
if ( $submission = WPCF7_Submission::get_instance() )
{
// 投稿データを取得
$posted_data = $submission->get_posted_data();
}
return $cf7;
}
ユーザーが入力した内容を変更する
add_filter('wpcf7_before_send_mail', 'my_wpcf7_before_send_mail', 1, 1);
function my_wpcf7_before_send_mail($cf7)
{
if ( $submission = WPCF7_Submission::get_instance() )
{
// 投稿データを取得
$posted_data = $submission->get_posted_data();
// 件名の変更
$posted_data['your-subject'] = 'メール件名を入力します';
// 本文の変更
$posted_data['your-message'] = 'メール本文を入力します';
// 投稿データを設定
// ※set_posted_data()はデフォルトでは存在しないのでsubmission.phpに追加する
$submission->set_posted_data($posted_data);
}
return $cf7;
}
以下をsubmission.phpに追記する。
※もしかしたらContactForm7のアップデートの際に消えるかも。(確かめてないです)
public function set_posted_data( $properties ) {
$defaults = $this->get_posted_data();
$properties = wp_code_parse_args( $properties, $defaults );
$properties = array_intersect_key( $properties, $defaults );
$this->posted_data = $properties;
}
