概要
woocommerce_email_headers
フィルタは、WooCommerceが送信するメールのヘッダーをカスタマイズするために使用されます。このフィルタを利用することで、送信元メールアドレスやメールの件名、その他のカスタムヘッダーを追加することが可能です。主に次のような機能を実装する際によく使用されます。
- メールの送信元アドレスを変更する
- メールの件名をカスタマイズする
- カスタムヘッダーを追加する(例: CCやBCC)
- 特定の条件下でヘッダーの内容を調整する
- トラッキング用のヘッダーを追加する
- スパムフィルタ対策のためのヘッダーを設定する
構文
add_filter( 'woocommerce_email_headers', 'custom_email_headers', 10, 3 );
パラメータ
woocommerce_email_headers
:フィルターフックの名前custom_email_headers
:カスタマイズする関数名10
:優先度(低い数値ほど優先される)3
:渡される引数の数
戻り値
- 変更されたメールヘッダーの内容(文字列)
使用可能なバージョン
- 使用可能なプラグインWooCommerceのバージョン:3.0.0以降
- WordPressのバージョン:4.0以降
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
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 |
サンプルコード
サンプルコード 1: 送信元アドレスを変更する
add_filter( 'woocommerce_email_headers', 'change_sender_email', 10, 3 );
function change_sender_email( $headers, $email_id, $order ) {
$headers .= 'From: Your Name <your-email@example.com>' . "rn";
return $headers;
}
このサンプルコードは、WooCommerceから送信されるメールの送信元アドレスを変更します。
サンプルコード 2: カスタムヘッダーを追加する
add_filter( 'woocommerce_email_headers', 'add_custom_email_header', 10, 3 );
function add_custom_email_header( $headers, $email_id, $order ) {
$headers .= 'X-Custom-Header: CustomValue' . "rn";
return $headers;
}
ここでは、WooCommerceメールにカスタムヘッダー「X-Custom-Header」を追加します。
サンプルコード 3: BCCヘッダーを追加する
add_filter( 'woocommerce_email_headers', 'add_bcc_to_email_headers', 10, 3 );
function add_bcc_to_email_headers( $headers, $email_id, $order ) {
$headers .= 'Bcc: bcc-recipient@example.com' . "rn";
return $headers;
}
このサンプルは、BCCとして別の受信者のメールアドレスを追加する方法を示しています。
サンプルコード 4: メールの件名を変更する
add_filter( 'woocommerce_email_subject_new_order', 'custom_new_order_subject', 10, 2 );
function custom_new_order_subject( $subject, $order ) {
return '新しい注文がありました: ' . $subject;
}
このコードは、新しい注文メールの件名をカスタマイズしています。
サンプルコード 5: 特定のメールタイプのヘッダーを条件付きで変更する
add_filter( 'woocommerce_email_headers', 'conditional_email_headers', 10, 3 );
function conditional_email_headers( $headers, $email_id, $order ) {
if ( $email_id == 'customer_completed_order' ) {
$headers .= 'From: Completed Order <completed@example.com>' . "rn";
}
return $headers;
}
このサンプルでは、特定のメールタイプ(完了した注文)のヘッダーを条件に応じて変更しています。