Using WordPress ‘after_signup_user’ PHP action

The after_signup_user WordPress PHP action fires after a user’s signup information has been written to the database.

Usage

add_action('after_signup_user', 'your_custom_function', 10, 4);

function your_custom_function($user, $user_email, $key, $meta) {
    // Your custom code here
}

Parameters

  • $user (string) – The user’s requested login name.
  • $user_email (string) – The user’s email address.
  • $key (string) – The user’s activation key.
  • $meta (array) – Signup meta data. Default empty array.

More information

See WordPress Developer Resources: after_signup_user

Examples

Send a welcome email after signup

Send a custom welcome email to the user after they sign up.

add_action('after_signup_user', 'send_welcome_email', 10, 4);

function send_welcome_email($user, $user_email, $key, $meta) {
    // Compose the email
    $subject = 'Welcome to My Website!';
    $message = "Hello {$user},\n\nThank you for signing up on our website.\n\nPlease activate your account using the following link:\n\nhttp://example.com/activate?key={$key}\n\nBest regards,\nMy Website Team";

    // Send the email
    wp_mail($user_email, $subject, $message);
}

Add custom user meta after signup

Store custom user meta information after a user signs up.

add_action('after_signup_user', 'store_custom_user_meta', 10, 4);

function store_custom_user_meta($user, $user_email, $key, $meta) {
    // Add custom meta data to $meta array
    $meta['custom_field'] = 'Custom Value';

    // Update the user meta data in the database
    update_user_meta($user, 'custom_field', $meta['custom_field']);
}

Log user signups

Log user signups for further analysis.

add_action('after_signup_user', 'log_user_signup', 10, 4);

function log_user_signup($user, $user_email, $key, $meta) {
    // Log user signup details
    error_log("User '{$user}' with email '{$user_email}' signed up with activation key '{$key}'.");
}

Add user to a mailing list

Add a new user to a mailing list after they sign up.

add_action('after_signup_user', 'add_user_to_mailing_list', 10, 4);

function add_user_to_mailing_list($user, $user_email, $key, $meta) {
    // Add user to mailing list
    // Replace the following function with the relevant mailing list provider's API call
    your_mailing_list_provider_api_add_user($user_email);
}

Track user signups using analytics

Track user signups using an analytics tool.

add_action('after_signup_user', 'track_user_signup', 10, 4);

function track_user_signup($user, $user_email, $key, $meta) {
    // Track user signup using an analytics tool
    // Replace the following function with the relevant analytics provider's tracking code
    your_analytics_provider_api_track_signup($user_email);
}