Skip to content

Instantly share code, notes, and snippets.

@onocom
Last active January 15, 2023 14:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onocom/15b653713648794fc6c2 to your computer and use it in GitHub Desktop.
Save onocom/15b653713648794fc6c2 to your computer and use it in GitHub Desktop.
コンタクトフォーム7でフォームの送信先を動的に変更したい機会があり、無理やり書き換えることを試みた。
<?php
define( "O_SYSTEM_CRYPT_KEY" , "OrehaJaian!Gakidaisho!OiNobitaButtobasuzo!" ); // 暗号化キー
define( "REPLACE_MAIL_ADDRESS" , "replace-email-address@example.com" ); // 置換対象となるメールアドレス
// コンタクトフォーム7にhiddenフィールドを追加
add_filter( 'wpcf7_form_hidden_fields', 'oc_wpcf7_form_hidden_fields');
function oc_wpcf7_form_hidden_fields( $hidden ) {
$email = "hogehoge@example.com"; // 送信したいアドレスをhiddenフィールドに暗号化して設定しておく
$cript_mail = openssl_encrypt($email, 'AES-128-ECB', O_SYSTEM_CRYPT_KEY);
$hidden["_wpcf7_mail"] = $cript_author;
return $hidden;
}
// 送信時に書き換え
add_filter( 'wpcf7_mail_components', 'oc_wpcf7_mail_components');
function oc_wpcf7_mail_components( $components ) {
if( $components['recipient'] == REPLACE_MAIL_ADDRESS && $_POST['_wpcf7_mail'] ) {
$cript_mail = $_POST['_wpcf7_mail'];
$email = openssl_decrypt($cript_mail, 'AES-128-ECB', O_SYSTEM_CRYPT_KEY);
if( $email ) {
$components['recipient'] = $email;
}
}
return $components;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment