概要
wpcf7_collect_mail_tags
フィルタは、Contact Form 7 プラグインを用いて独自のメールタグを設定する際に使用されます。このフィルタを利用することで、メールの送信内容にカスタムタグを追加し、より柔軟なメールフォーマットを作成できます。特に以下のような機能を実装する際によく使われます。
- カスタムフィールドの値をメールに含める
- 条件に基づいたメール内容の変更
- 特定のユーザー情報をメールに追加
- サイトの設定情報をメールに含める
- 他のプラグインからデータを取得してメールに反映
- 特別なフォーマットでメールを送信する
構文
apply_filters( 'wpcf7_collect_mail_tags', $mail_tags, $form )
パラメータ
$mail_tags
: メールタグの配列$form
: 現在のフォームオブジェクト
戻り値
- 上書きされたメールタグの配列
使用可能なプラグインバージョン
- Contact Form 7: 5.0以上
使用可能なワードプレスのバージョン
- WordPress: 4.0以上
サンプルコード
サンプルコード 1
add_filter( 'wpcf7_collect_mail_tags', 'custom_mail_tag_example', 10, 2 );
function custom_mail_tag_example( $mail_tags, $form ) {
$mail_tags['custom_tag'] = 'これはカスタムタグの値です';
return $mail_tags;
}
このサンプルコードは、custom_tag
という独自のメールタグを作成し、固定の値を設定しています。
サンプルコード 2
add_filter( 'wpcf7_collect_mail_tags', 'dynamic_mail_tag_example', 10, 2 );
function dynamic_mail_tag_example( $mail_tags, $form ) {
$current_user = wp_get_current_user();
if ( $current_user->exists() ) {
$mail_tags['user_email'] = $current_user->user_email;
}
return $mail_tags;
}
このサンプルコードでは、現在ログインしているユーザーのメールアドレスを独自のメールタグ user_email
に設定しています。
サンプルコード 3
add_filter( 'wpcf7_collect_mail_tags', 'conditional_mail_tag_example', 10, 2 );
function conditional_mail_tag_example( $mail_tags, $form ) {
if ( isset( $_POST['special_field'] ) && $_POST['special_field'] === 'yes' ) {
$mail_tags['special_info'] = '特別な情報が必要です';
}
return $mail_tags;
}
このサンプルコードは、フォームの特定のフィールドに基づいて独自のメールタグ special_info
を追加します。
サンプルコード 4
add_filter( 'wpcf7_collect_mail_tags', 'post_data_mail_tag_example', 10, 2 );
function post_data_mail_tag_example( $mail_tags, $form ) {
$mail_tags['post_title'] = get_the_title( get_the_ID() );
return $mail_tags;
}
このサンプルコードは、現在の投稿のタイトルを独自のメールタグ post_title
に追加します。
サンプルコード 5
add_filter( 'wpcf7_collect_mail_tags', 'site_info_mail_tag_example', 10, 2 );
function site_info_mail_tag_example( $mail_tags, $form ) {
$mail_tags['site_name'] = get_bloginfo( 'name' );
return $mail_tags;
}
このサンプルコードは、サイトの名称を独自のメールタグ site_name
に格納します。
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
mu_plugin_loaded | |
registered_post_type | |
plugins_loaded | |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | |
get_header | |
wp_head |
この表は、wpcf7_collect_mail_tags
フィルタがどのアクションで使用されているかの可用性を示しています。