Using WordPress ‘grant_super_admin()’ PHP function

The grant_super_admin() WordPress PHP function grants Super Admin privileges to a specified user.

Usage

To use the grant_super_admin() function, simply pass the user ID of the user you want to grant Super Admin privileges to:

grant_super_admin( $user_id );

Parameters

  • $user_id (int): Required. ID of the user to be granted Super Admin privileges.

More information

See WordPress Developer Resources: grant_super_admin()

Examples

Granting Super Admin privileges to a user

In this example, we’re granting Super Admin privileges to a user with the ID of 5.

// Grant Super Admin privileges to user with ID 5
grant_super_admin( 5 );

Granting Super Admin privileges using a username

Here, we’re looking up a user by their username and granting them Super Admin privileges.

// Get user by username
$user = get_user_by( 'login', 'johndoe' );

// Grant Super Admin privileges
if ( $user ) {
    grant_super_admin( $user->ID );
}

Granting Super Admin privileges to the current user

In this example, we’re granting Super Admin privileges to the currently logged-in user.

// Get current user
$current_user = wp_get_current_user();

// Grant Super Admin privileges
grant_super_admin( $current_user->ID );

Granting Super Admin privileges to a new user

Here, we’re creating a new user and then granting them Super Admin privileges.

// Create a new user
$new_user_id = wp_create_user( 'newuser', 'password123', '[email protected]' );

// Grant Super Admin privileges to the new user
grant_super_admin( $new_user_id );

Checking if a user is a Super Admin before granting privileges

In this example, we’re checking if a user is already a Super Admin before granting them Super Admin privileges.

// User ID
$user_id = 7;

// Check if user is already a Super Admin
if ( ! is_super_admin( $user_id ) ) {
    // Grant Super Admin privileges
    grant_super_admin( $user_id );
}

Tagged in

Leave a Comment

Your email address will not be published. Required fields are marked *