WordPress API: Add super user (site admin)

I can add an administrator like this:

$password = wp_generate_password();
$userData = array(
    'user_pass'     => $password,
    'user_login'    => $email,
    'user_nicename' => sanitize_title($name),
    'user_email'    => $email,
    'display_name'  => $name,
    'role'      => 'administrator'
);
$user_id = wp_insert_user($userData);

That works fine, but how do I add a super user? What is the role name?

Read More

I tried ‘role’ => ‘super user’, ‘super_user’, ‘superuser’, … but nothing works.

Related posts

Leave a Reply

4 comments

  1. The other answers are basically correct, but it is better to use built-in functions from WordPress whenever possible. The function you are looking for is grant_super_admin().

    Based on your example code:

    $password = wp_generate_password();
    $userData = array(
        'user_pass'     => $password,
        'user_login'    => $email,
        'user_nicename' => sanitize_title($name),
        'user_email'    => $email,
        'display_name'  => $name,
        'role'          => 'administrator'
    );
    $user_id = wp_insert_user($userData);
    
    // Make the user a super admin.
    grant_super_admin( $user_id );
    
  2. There is no such role.

    The super admins are stored as site options as you can see in the function get_super_admins.
    In is_super_admin it is checked whether the user’s login is in the array returned by the former function.

    Usually this is only the user with the login name admin. What you need to to is

    $admins = get_super_admins();
    $admins[] = 'your_user_login@example.com';
    update_site_option('site_admins', $admins);
    
  3. This looks like it’s working:

    1) Set user role to “site_admin”

    2) Add user as administrator for every blog

    $blogs = get_blog_list(0, 'all');
    foreach ($blogs as $blog)
    {
        add_user_to_blog($blog['blog_id'], $user_id, 'administrator');
    }
    

    3) As Alex said, add to site_admins:

    $admins = get_super_admins();
    if (!in_array($email, $admins))
    {
        $admins[] = $email;
        update_site_option('site_admins', $admins);
    }