wp_send_new_user_notifications()


WordPressのwp_send_new_user_notifications()関数は、新規ユーザーが登録された際に通知メールを送信するために使用されます。管理者とユーザーに対して、それぞれ別々のメールが送信されます。

構文

wp_send_new_user_notifications( int $user_id, string $notify = 'both' );

引数の説明:

  • $user_id(int) — 通知を送信するユーザーのID。
  • $notify(string) — 通知の種類を指定します。’both’(デフォルト)は管理者とユーザー両方に通知を送信し、’admin’は管理者のみ、’user’はユーザーのみに通知を送信します。

例1: 新規ユーザー登録時に通知を送信

新規ユーザーが登録された際に、管理者とユーザーに通知メールを送信します。

wp_send_new_user_notifications( get_current_user_id() );

例2: 管理者のみに通知を送信

新規ユーザーが登録された際に、管理者のみに通知メールを送信します。

wp_send_new_user_notifications( get_current_user_id(), 'admin' );

例3: ユーザーのみに通知を送信

新規ユーザーが登録された際に、ユーザーのみに通知メールを送信します。

wp_send_new_user_notifications( get_current_user_id(), 'user' );

例4: カスタムユーザーIDを使用して通知を送信

特定のユーザーIDを使用して通知を送信します。

wp_send_new_user_notifications( 5 );

例5: ユーザー登録フックで通知を送信

ユーザー登録フックを使用して、自動的に通知を送信します。

add_action( 'user_register', function( $user_id ) { wp_send_new_user_notifications( $user_id ); } );

例6: 通知を送信しない場合

特定の条件下で通知を送信しないようにします。

if ( ! $some_condition ) { wp_send_new_user_notifications( get_current_user_id() ); }

例7: カスタムメールアドレスを使用して通知を送信

カスタムメールアドレスを使用して通知を送信します。

$user_id = wp_create_user( 'username', 'password', 'custom@example.com' ); wp_send_new_user_notifications( $user_id );

例8: 通知を送信する前にユーザーデータを変更

通知を送信する前に、ユーザーデータを変更します。

$user_id = wp_create_user( 'username', 'password', 'user@example.com' ); wp_update_user( array( 'ID' => $user_id, 'display_name' => 'New User' ) ); wp_send_new_user_notifications( $user_id );

注意点

  • この関数は、ユーザーが登録された直後に呼び出す必要があります。
  • 通知メールの内容は、WordPressの設定に依存します。
  • 通知メールの送信に失敗した場合、エラーログに記録されます。

関連機能: