プラグインWooCommerceのwoocommerce_email_headerアクションの使用方法・解説

概要

woocommerce_email_header アクションは、WooCommerce プラグインで送信されるすべてのメールのヘッダー部分をカスタマイズするためのフックです。このアクションを利用することで、メールのタイトルやスタイルを変更したり、特定の情報を追加したりすることが可能です。主に以下のような機能を実装する際によく使用されます。

  1. 企業ロゴの追加
  2. メールタイトルのフォーマットを変更
  3. メールヘッダーにカスタムテキストを追加
  4. 特定のスタイルシートを適用
  5. メールのトラッキング用の情報を追加
  6. ソーシャルメディアのリンクを挿入

このアクションは、WooCommerce バージョン 2.1 以降および WordPress バージョン 3.0 以降で使用可能です。

構文

add_action('woocommerce_email_header', 'your_custom_email_header', 10, 2);

パラメータ

  • $email_id: 送信されるメールの ID。
  • $email: WC_Email オブジェクト。

戻り値

このアクションは戻り値を持たず、メールのヘッダー部分を直接変更します。

サンプルコード

サンプルコード 1: ロゴを追加

add_action('woocommerce_email_header', 'add_logo_to_email_header', 10, 2);
function add_logo_to_email_header($email_id, $email) {
    echo '<img src="' . esc_url(get_bloginfo('template_directory') . '/images/logo.png') . '" alt="Logo" />';
}

このコードは、メールのヘッダーにウェブサイトのロゴを追加します。ロゴの画像パスはテーマディレクトリから取得しています。

出典: https://woocommerce.com/

サンプルコード 2: カスタムテキストの追加

add_action('woocommerce_email_header', 'add_custom_text_to_email_header');
function add_custom_text_to_email_header($email_id, $email) {
    echo '<h1>ご注文ありがとうございます!</h1>';
}

このコードは、メールのヘッダーに「ご注文ありがとうございます!」というカスタムテキストを追加します。

出典: https://woothemes.com/

サンプルコード 3: スタイルを追加

add_action('woocommerce_email_header', 'custom_email_styles');
function custom_email_styles($email_id, $email) {
    echo '<style>h1 { color: #ff0000; }</style>';
}

メールのヘッダーにスタイルを追加し、見出し(h1)の色を赤に変更します。

出典: https://woocommerce.com/

サンプルコード 4: トラッキング情報の挿入

add_action('woocommerce_email_header', 'add_tracking_info_to_email_header', 10, 2);
function add_tracking_info_to_email_header($email_id, $email) {
    echo '<p>お荷物の追跡は<a href="https://your-tracking-url.com">こちら</a>から。</p>';
}

このコードは、メールのヘッダー部分に荷物の追跡リンクを追加します。

出典: https://developer.wordpress.org/

サンプルコード 5: ソーシャルメディアリンクの追加

add_action('woocommerce_email_header', 'add_social_links_to_email_header');
function add_social_links_to_email_header($email_id, $email) {
    echo '<p>フォローしてね: <a href="https://twitter.com/yourprofile">Twitter</a> | <a href="https://facebook.com/yourprofile">Facebook</a></p>';
}

このコードでは、メールヘッダーにソーシャルメディアへのリンクを表示します。

出典: https://www.smashingmagazine.com/

この関数のアクションでの使用可能性

アクション 使用例
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

この関数について質問する


上の計算式の答えを入力してください