概要
フィルタ woocommerce_email_content_type
は、WooCommerce のメールテンプレートにおけるコンテンツのタイプを変更するために使用されます。これを利用することで、特定のメールに対してテキスト形式や HTML 形式を適用したり、カスタムなメール出力形式を設定したりすることが可能です。よく使われる機能には以下があります。
- 注文確認メールの形式をHTMLに変更
- 顧客へのダウンロードリンクメールをカスタマイズ
- 定期購読に関する通知メールのフォーマットを調整
- ショッピングカート放棄リマインダーの送信形式を指定
- ギフトカード発行メールの内容を変更
- 誕生日クーポンや特別プロモーションメールの形式をカスタマイズ
構文
add_filter('woocommerce_email_content_type', 'my_custom_email_content_type');
パラメータ
- $content_type: 変更されるメールのコンテンツタイプ(デフォルトは
'text/html'
)
戻り値
- string: 設定されたコンテンツタイプ
使用可能なプラグインWooCommerceのバージョン
- バージョン: WooCommerce 2.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. HTMLメールとテキストメールの切り替え
add_filter('woocommerce_email_content_type', 'set_content_type');
function set_content_type($content_type) {
if (is_admin()) {
return 'text/plain'; // 管理画面ではテキスト形式に
}
return 'text/html'; // フロントエンドではHTML形式
}
このサンプルコードは、管理画面ではテキスト形式を、それ以外ではHTML形式を利用するようにメールのコンテンツタイプを切り替えます。
2. 特定のメールのみHTML形式に設定
add_filter('woocommerce_email_content_type', 'custom_order_email_content_type');
function custom_order_email_content_type($content_type) {
global $pagenow;
if ('admin-post.php' === $pagenow) {
return 'text/html'; // 管理投稿時のメールはHTML形式
}
return $content_type; // それ以外はデフォルト
}
このコードは、特定の管理投稿時にのみHTML形式を選択することで、他のメールには影響を与えないようにします。
3. 特定の条件下でメール形式を変更
add_filter('woocommerce_email_content_type', 'conditional_email_format');
function conditional_email_format($content_type) {
if (is_user_logged_in()) {
return 'text/html'; // ログインユーザーに送信するメールはHTML形式
}
return 'text/plain'; // その他はテキスト形式
}
このサンプルでは、ログインしているユーザーに対してだけHTML形式のメールを送るようにします。
4. 新規顧客向けの歓迎メールをHTML形式に設定
add_filter('woocommerce_email_content_type', 'welcome_email_content_type');
function welcome_email_content_type($content_type) {
if (isset($_GET['welcome_email'])) {
return 'text/html'; // ウェルカムメールはHTML形式
}
return $content_type; // その他はデフォルト
}
このコードは、ウェルカムメールの場合にのみHTML形式を採用することで、特別なメールに対するカスタマイゼーションを行います。
5. お知らせメールをテキスト形式に設定
add_filter('woocommerce_email_content_type', 'notification_email_content_type');
function notification_email_content_type($content_type) {
if (has_action('woocommerce_notification_email')) {
return 'text/plain'; // 通知メールはテキスト形式
}
return $content_type; // 他はデフォルト
}
このサンプルコードは、通知メールが送信される際に、そのメールの形式をテキストに変更します。