Using WordPress ‘grant_super_admin’ PHP action

The grant_super_admin WordPress PHP action fires before a user is granted Super Admin privileges.

Super admins have the ability to manage all aspects of a WordPress site, including network administration for multisite installations.

Usage

add_action('grant_super_admin', 'your_custom_function', 10, 1);
function your_custom_function($user_id) {
    // Your custom code here
}

Parameters

  • $user_id (int) – ID of the user that is about to be granted Super Admin privileges.

More information

See WordPress Developer Resources: grant_super_admin

Examples

Send a welcome email to new Super Admins

Sends a welcome email to the user when they are granted Super Admin privileges.

add_action('grant_super_admin', 'send_welcome_email', 10, 1);
function send_welcome_email($user_id) {
    $user_info = get_userdata($user_id);
    $email = $user_info->user_email;
    $subject = 'Welcome to Super Admin!';
    $message = 'Hi ' . $user_info->display_name . ', you have been granted Super Admin privileges. Welcome to the team!';
    wp_mail($email, $subject, $message);
}

Log Super Admin promotions

Logs when a user is granted Super Admin privileges to a custom log file.

add_action('grant_super_admin', 'log_super_admin_promotion', 10, 1);
function log_super_admin_promotion($user_id) {
    $user_info = get_userdata($user_id);
    $log_message = 'User ' . $user_info->display_name . ' (' . $user_id . ') was granted Super Admin privileges.';
    error_log($log_message, 3, '/path/to/your/custom_log.log');
}

Add a custom role to new Super Admins

Automatically adds a custom role to users when they are granted Super Admin privileges.

add_action('grant_super_admin', 'add_custom_role', 10, 1);
function add_custom_role($user_id) {
    $user = new WP_User($user_id);
    $user->add_role('custom_role');
}

Notify site administrators of new Super Admins

Sends an email notification to all site administrators when a new user is granted Super Admin privileges.

add_action('grant_super_admin', 'notify_admins_new_super_admin', 10, 1);
function notify_admins_new_super_admin($user_id) {
    $user_info = get_userdata($user_id);
    $subject = 'New Super Admin: ' . $user_info->display_name;
    $message = 'User ' . $user_info->display_name . ' (' . $user_id . ') has been granted Super Admin privileges.';
    $admins = get_users(['role' => 'administrator']);
    foreach ($admins as $admin) {
        wp_mail($admin->user_email, $subject, $message);
    }
}

Restrict Super Admin privileges to specific user IDs

Prevents users with certain IDs from being granted Super Admin privileges.

add_action('grant_super_admin', 'restrict_super_admin', 10, 1);
function restrict_super_admin($user_id) {
    $restricted_ids = [1, 5, 10];
    if (in_array($user_id, $restricted_ids)) {
        wp_die('This user cannot be granted Super Admin privileges.');
    }
}