WordPressを使ってお問合せフォームといえばContact Form 7というぐらいに定番になっているかと思いますが,単純なお問合せフォームでも色々と条件分岐したり,置換してみたりしたいことが出てきます。
条件分岐や置換の部分をPHPで書けば良いのですが,さてそのフィルタをどこに入れようかと,ソースを追いかけると,ちゃんと用意されておりました。
contact-form-7/includes/class.php 内の compose_mail 内で
$components = compact(
'subject', 'sender', 'body', 'recipient', 'additional_headers', 'attachments' );
$components = apply_filters_ref_array( 'wpcf7_mail_components',
array( $components, &$this ) );
extract( $components );
というのがありましたので,テーマ内のfunctions.phpにこんな感じで追加
add_filter('wpcf7_mail_components', 'my_mail_filter',10,2);
function my_mail_filter( $components, $cf7) {
$lines=explode("\n",$components['body']);
$newbody='';
foreach($lines as $l){
//行毎に分解して処理する感じのコード
//例えば半角カナを全角に変換とか
$newbody.=mb_convert_kana($l,'aKV')."\n";
}
$components['body']=$newbody;
return $components;
}
あとは自分の関数内で書き放題ということで。