Using WordPress ‘network_user_new_created_user’ PHP action

The network_user_new_created_user WordPress action fires after a new user has been created via the network user-new.php page.

Usage

add_action('network_user_new_created_user', 'my_custom_function', 10, 1);

function my_custom_function($user_id) {
  // your custom code here
}

Parameters

  • $user_id (int) – ID of the newly created user.

More information

See WordPress Developer Resources: network_user_new_created_user

Examples

Send welcome email to new user

Send a welcome email to the newly created user.

add_action('network_user_new_created_user', 'send_welcome_email', 10, 1);

function send_welcome_email($user_id) {
  $user_info = get_userdata($user_id);
  $to = $user_info->user_email;
  $subject = 'Welcome to Our Website';
  $message = 'Hi, welcome to our website! We are glad to have you on board.';
  wp_mail($to, $subject, $message);
}

Assign default role to new user

Assign a default role to the newly created user.

add_action('network_user_new_created_user', 'assign_default_role', 10, 1);

function assign_default_role($user_id) {
  $default_role = 'subscriber';
  $user = new WP_User($user_id);
  $user->set_role($default_role);
}

Log new user creation

Log the creation of new users to a custom log file.

add_action('network_user_new_created_user', 'log_new_user_creation', 10, 1);

function log_new_user_creation($user_id) {
  $user_info = get_userdata($user_id);
  $log_message = 'New user created: ' . $user_info->user_login . ' (' . $user_info->user_email . ')';
  error_log($log_message, 3, '/path/to/your/custom.log');
}

Notify admin of new user

Notify the admin when a new user is created.

add_action('network_user_new_created_user', 'notify_admin_new_user', 10, 1);

function notify_admin_new_user($user_id) {
  $user_info = get_userdata($user_id);
  $admin_email = get_option('admin_email');
  $subject = 'New User Registration';
  $message = 'A new user has registered: ' . $user_info->user_login . ' (' . $user_info->user_email . ')';
  wp_mail($admin_email, $subject, $message);
}

Set custom meta data for new user

Set custom meta data for the newly created user.

add_action('network_user_new_created_user', 'set_custom_user_meta', 10, 1);

function set_custom_user_meta($user_id) {
  $custom_meta_key = 'my_custom_key';
  $custom_meta_value = 'my_custom_value';
  update_user_meta($user_id, $custom_meta_key, $custom_meta_value);
}

Send a welcome email to the new user

Send a welcome email to the new user after their account has been created.

add_action('network_user_new_created_user', 'send_welcome_email', 10, 1);

function send_welcome_email($user_id) {
    $user_info = get_userdata($user_id);
    $user_email = $user_info->user_email;

    wp_mail($user_email, 'Welcome to Our Network', 'Hello, and welcome to our network!');
}

Add new user to a specific blog

Automatically add the newly created user to a specific blog in the network.

add_action('network_user_new_created_user', 'add_user_to_blog', 10, 1);

function add_user_to_blog($user_id) {
    $blog_id = 2; // The ID of the blog to add the user to
    $role = 'subscriber';

    add_user_to_blog($blog_id, $user_id, $role);
}

Set custom user meta

Set custom user meta for the new user.

add_action('network_user_new_created_user', 'set_custom_user_meta', 10, 1);

function set_custom_user_meta($user_id) {
    update_user_meta($user_id, 'custom_key', 'custom_value');
}

Log new user creation

Log the new user creation in a separate log file.

add_action('network_user_new_created_user', 'log_new_user', 10, 1);

function log_new_user($user_id) {
    $logfile = 'new_users.log';
    $logdata = "New user created with ID: $user_id\n";

    file_put_contents($logfile, $logdata, FILE_APPEND);
}

Add new user to a mailing list

Add the new user’s email to a mailing list after their account has been created.

add_action('network_user_new_created_user', 'add_user_to_mailing_list', 10, 1);

function add_user_to_mailing_list($user_id) {
    $user_info = get_userdata($user_id);
    $user_email = $user_info->user_email;

    // Replace 'your_mailing_list_api_function' with the function provided by your mailing list service
    your_mailing_list_api_function($user_email);
}