Using WordPress ‘network_site_users_created_user’ PHP action

The network_site_users_created_user WordPress PHP action fires after a user has been created via the network site-users.php page.

Usage

add_action('network_site_users_created_user', 'your_custom_function', 10, 1);

function your_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_site_users_created_user

Examples

Send a welcome email to the new user

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

function send_welcome_email($user_id) {
    $user = get_user_by('id', $user_id);
    $to = $user->user_email;
    $subject = 'Welcome to our Network';
    $message = 'Hello, ' . $user->display_name . '! Welcome to our website network.';

    wp_mail($to, $subject, $message);
}

Add a custom role to the new user

add_action('network_site_users_created_user', 'add_custom_role_to_new_user', 10, 1);

function add_custom_role_to_new_user($user_id) {
    $user = new WP_User($user_id);
    $user->add_role('your_custom_role');
}

Log user creation in a custom table

add_action('network_site_users_created_user', 'log_user_creation', 10, 1);

function log_user_creation($user_id) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'user_creation_log';

    $wpdb->insert(
        $table_name,
        array(
            'user_id' => $user_id,
            'created_at' => current_time('mysql')
        )
    );
}

Set a custom user meta upon registration

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

function set_custom_user_meta($user_id) {
    update_user_meta($user_id, 'your_custom_meta_key', 'your_custom_meta_value');
}

Assign the new user to a specific blog in the network

add_action('network_site_users_created_user', 'assign_user_to_blog', 10, 1);

function assign_user_to_blog($user_id) {
    $blog_id = 2; // replace with the desired blog ID
    add_user_to_blog($blog_id, $user_id, 'subscriber');
}