Using WordPress ‘pre_network_site_new_created_user’ PHP action

The pre_network_site_new_created_user WordPress PHP action fires immediately before a new user is created via the network site-new.php page.

Usage

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

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

Parameters

  • $email (string) – Email of the non-existent user.

More information

See WordPress Developer Resources: pre_network_site_new_created_user

Examples

Log new user email

Log the email address of the new user being created.

add_action('pre_network_site_new_created_user', 'log_new_user_email', 10, 1);

function log_new_user_email($email) {
  error_log("New user email: " . $email);
}

Send a welcome email

Send a welcome email to the new user.

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

function send_welcome_email($email) {
  $subject = "Welcome to our network!";
  $message = "Hello! Thank you for joining our network. We're glad to have you!";
  wp_mail($email, $subject, $message);
}

Validate email domain

Prevent user registration if the email domain is not allowed.

add_action('pre_network_site_new_created_user', 'validate_email_domain', 10, 1);

function validate_email_domain($email) {
  $allowed_domain = "example.com";
  $email_domain = substr(strrchr($email, "@"), 1);

  if ($email_domain !== $allowed_domain) {
    wp_die('Email domain not allowed.');
  }
}

Add new user to external service

Add the new user’s email to an external mailing list or CRM.

add_action('pre_network_site_new_created_user', 'add_new_user_to_external_service', 10, 1);

function add_new_user_to_external_service($email) {
  // API call to add email to external service
}

Store user creation timestamp

Store the timestamp of when the new user was created in the user’s metadata.

add_action('pre_network_site_new_created_user', 'store_user_creation_timestamp', 10, 1);

function store_user_creation_timestamp($email) {
  $user = get_user_by('email', $email);
  $timestamp = time();
  update_user_meta($user->ID, 'user_creation_timestamp', $timestamp);
}