ワードプレスのwp_new_user_notificationアクションの使用方法・解説

概要

wp_new_user_notificationアクションは、ユーザが登録されたことを管理者とユーザに通知する際に使用されるフックです。このアクションは、ユーザが登録された直後に実行されます。例えば、ユーザが新しいアカウントを作成したときに、確認メールや歓迎メールを送信する機能を実装する際に利用されます。具体的には、以下のような状況でよく使われます:

  1. 新規ユーザ登録時のウェルカムメール送信
  2. ユーザ登録の確認メールの送信
  3. 管理者への新規ユーザ登録通知
  4. ユーザの役割に応じたカスタマイズされたメール送信
  5. 登録時の自動処理や追加データの保存
  6. 外部サービスへの登録通知(例: CRMとの連携)
  7. ユーザの登録をトラッキングするためのログ記録
  8. 特定の条件に基づいたカスタムアクションのトリガー

このアクションは、WordPressのインストール時に自動で呼び出されます。

構文

do_action('wp_new_user_notification', $user_id, null);

パラメータ

  • $user_id (int) – 新しく登録されたユーザのユーザID。
  • $plaintext_pass (string, optional) – 新規ユーザの平文パスワード(デフォルトはnull)。

戻り値

特定の戻り値はなく、ただアクションが実行されることを目的としています。

関連する関数

wp_new_user_notification

使用可能なバージョン

このアクションはWordPress 3.0.0以降で使用可能です。

コアファイルのパス

wp-includes/user.php

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

アクション 使用例
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_action('wp_new_user_notification', 'send_custom_welcome_email', 10, 2);

function send_custom_welcome_email($user_id, $plaintext_pass) {
    $user_info = get_userdata($user_id);
    $to = $user_info->user_email;
    $subject = 'ようこそ!';
    $message = 'あなたのアカウントが作成されました。';
    wp_mail($to, $subject, $message);
}

このコードは、新規ユーザが登録された際に、カスタマイズされたウェルカムメールを送信します。

サンプル2:新規ユーザ情報を管理者に通知

add_action('wp_new_user_notification', 'notify_admin_of_new_user', 10, 1);

function notify_admin_of_new_user($user_id) {
    $admin_email = get_option('admin_email');
    $user_info = get_userdata($user_id);
    $message = '新しいユーザが登録されました:' . $user_info->user_login;
    wp_mail($admin_email, '新規ユーザ登録', $message);
}

このコードは、新しく登録されたユーザの情報を管理者にメール通知します。

サンプル3:ユーザ役割に応じた異なるメールを送信

add_action('wp_new_user_notification', 'role_based_welcome_email', 10, 2);

function role_based_welcome_email($user_id, $plaintext_pass) {
    $user_info = get_userdata($user_id);
    $role = $user_info->roles[0];

    $to = $user_info->user_email;
    $subject = 'ようこそ、' . ucfirst($role) . '!';
    $message = 'あなたの役割は ' . ucfirst($role) . ' です。';
    wp_mail($to, $subject, $message);
}

このコードは、ユーザの役割に応じて異なるウェルカムメールを送信します。

サンプル4:ユーザ登録情報をデータベースにログ保存

add_action('wp_new_user_notification', 'log_new_user_registration', 10, 1);

function log_new_user_registration($user_id) {
    $user_info = get_userdata($user_id);
    $log_message = '新しいユーザ ' . $user_info->user_login . ' が登録されました。';
    error_log($log_message);
}

このコードは、新規ユーザの登録をログに記録します。

サンプル5:ユーザ登録に際して外部APIに通知

add_action('wp_new_user_notification', 'notify_external_api', 10, 1);

function notify_external_api($user_id) {
    $user_info = get_userdata($user_id);
    $api_url = 'https://example.com/api/user_registered';
    $data = array('username' => $user_info->user_login, 'email' => $user_info->user_email);

    wp_remote_post($api_url, array('body' => json_encode($data), 'headers' => array('Content-Type' => 'application/json')));
}

このコードは、新規ユーザ登録時に外部APIに通知を送信します。

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


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