Using WordPress ‘newuser_notify_siteadmin()’ PHP function

The newuser_notify_siteadmin() WordPress PHP function notifies the network admin when a new user has been activated.

Usage

newuser_notify_siteadmin($user_id);

Input: $user_id – The ID of the newly activated user.

Output: Sends an email notification to the network admin.

Parameters

  • $user_id (int) – The new user’s ID, required for notifying the network admin.

More information

See WordPress Developer Resources: newuser_notify_siteadmin

Examples

Notify admin after a new user has been added

When a new user is added to the site, this code will send a notification to the network admin.

// Add the new user
$new_user_id = wp_create_user('example_user', 'example_password', '[email protected]');

// Notify the admin about the new user
newuser_notify_siteadmin($new_user_id);

Notify admin after a user is approved

If your site requires manual user approval, you can send a notification to the network admin once a user is approved.

// Function to approve a user
function approve_user($user_id) {
    // Set user as approved
    update_user_meta($user_id, 'is_approved', true);

    // Notify the admin about the approved user
    newuser_notify_siteadmin($user_id);
}

// Approve a specific user
$user_id = 123;
approve_user($user_id);

Customizing the notification email content

To modify the content of the notification email, use the newuser_notify_siteadmin filter.

// Function to customize the email content
function custom_newuser_email_content($email_content, $user_id) {
    $user = get_userdata($user_id);
    $email_content = "Hello admin,\n\nA new user, " . $user->user_login . ", has been activated on your website.\n\nRegards,\nYour Friendly WordPress";
    return $email_content;
}
add_filter('newuser_notify_siteadmin', 'custom_newuser_email_content', 10, 2);

Notifying admin when a user updates their profile

This example sends a notification to the network admin when a user updates their profile.

// Function to run when a user updates their profile
function notify_admin_on_profile_update($user_id) {
    newuser_notify_siteadmin($user_id);
}
add_action('profile_update', 'notify_admin_on_profile_update');

Notifying admin when a user role is changed

When a user’s role is changed, you can send a notification to the network admin using this example.

// Function to run when a user's role has changed
function notify_admin_on_role_change($user_id, $role) {
    newuser_notify_siteadmin($user_id);
}
add_action('set_user_role', 'notify_admin_on_role_change', 10, 2);