The add_user_to_blog WordPress PHP action fires immediately after a user is added to a site.
Usage
add_action('add_user_to_blog', 'your_custom_function', 10, 3); function your_custom_function($user_id, $role, $blog_id) { // your custom code here }
Parameters
$user_id
: (int) User ID.$role
: (string) User role.$blog_id
: (int) Blog ID.
More information
See WordPress Developer Resources: add_user_to_blog
Examples
Welcome Email
Send a welcome email to the new user.
add_action('add_user_to_blog', 'send_welcome_email', 10, 3); function send_welcome_email($user_id, $role, $blog_id) { // Get user's email address $user_info = get_userdata($user_id); $user_email = $user_info->user_email; // Prepare email content $subject = 'Welcome to ' . get_bloginfo('name'); $message = 'Hello ' . $user_info->display_name . ",\n\nWelcome to our website!\n\nBest regards,\nThe Team"; // Send email wp_mail($user_email, $subject, $message); }
Log User Addition
Log when a new user is added to the site.
add_action('add_user_to_blog', 'log_user_addition', 10, 3); function log_user_addition($user_id, $role, $blog_id) { // Get current time $timestamp = current_time('mysql'); // Log the event error_log("User {$user_id} with role {$role} added to blog {$blog_id} on {$timestamp}"); }
Assign Default User Meta
Assign default meta values to the new user.
add_action('add_user_to_blog', 'assign_default_user_meta', 10, 3); function assign_default_user_meta($user_id, $role, $blog_id) { // Set default user meta values update_user_meta($user_id, 'user_custom_field', 'Default Value'); }
Notify Admin
Notify the admin when a new user is added.
add_action('add_user_to_blog', 'notify_admin_new_user', 10, 3); function notify_admin_new_user($user_id, $role, $blog_id) { // Get admin email $admin_email = get_option('admin_email'); // Get user info $user_info = get_userdata($user_id); // Prepare email content $subject = "New User Added to " . get_bloginfo('name'); $message = "A new user has been added to your website:\n\nName: {$user_info->display_name}\nEmail: {$user_info->user_email}\nRole: {$role}"; // Send email to admin wp_mail($admin_email, $subject, $message); }
Update User Count
Update a custom user count on the site.
add_action('add_user_to_blog', 'update_user_count', 10, 3); function update_user_count($user_id, $role, $blog_id) { // Get current user count $user_count = get_option('custom_user_count', 0); // Increment user count $user_count++; // Update user count option update_option('custom_user_count', $user_count); }