0

I have put this code inside PHP file and put that file in root folder. When I run this file it Create "Admin" user for only main site. I want code that will create "super admin" which can access all the multi-site.

require ('wp-blog-header.php');
$newusername = 'USERNAME';
$newpassword = 'PASSWORD';
$newemail = 'EMAIL';

if ($newpassword != ' ' && $newemail != ' ' && $newusername != ' ') {

    if (!username_exists($newusername) && !email_exists($newemail)) {

        $user_id = wp_create_user($newusername, $newpassword, $newemail);
        if (is_int($user_id)) {
            $wp_user_object = new WP_User($user_id);
            $wp_user_object->set_role('administrator');
            echo 'Successfully created new admin user.';
        } else {
            echo 'Error with wp_insert_user. No users were created.';
        }

    } else {
        echo 'This user or email already exists. Nothing was done.';
    }

} else {
    echo "Whoops, looks like you didn't set a password, username, or 
         email before running the script. Set these variables and try again.";
}
2
  • But above code will add "Admin" to main site only. So how to access multi-site ? or how to add "Admin" user to all multi-site. ? Commented Mar 6, 2018 at 9:16
  • Please review my second link settings
    – Ravi Patel
    Commented Mar 6, 2018 at 10:24

2 Answers 2

0

More information for how to manage user manually

Manage This settings

 if ( $newpassword != ' ' && $newemail != ' ' && $newusername !=' '  ) {

         if ( !username_exists($newusername) && !email_exists($newemail) ) {

               $user_id = wp_create_user( $newusername, $newpassword, $newemail);
               if ( is_int($user_id) ) {
                       $wp_user_object = new WP_User($user_id);
                       $wp_user_object->set_role('administrator');

                       grant_super_admin($user_id); // Grants Super Admin privileges. 

                       echo 'Successfully created new admin user.';
       } else {
                    echo 'Error with wp_insert_user. No users were created.';
            }
      } else {
              echo 'This user or email already exists. Nothing was done.';
    } 
 } else {
           echo "Whoops, looks like you didn't set a password, username, or 
     email before running the script. Set these variables and try again.";
}
3
  • Well this code will create new user with labeled "Super Admin" which allow to edit "network admin" and Main site but not sub-site. Commented Mar 6, 2018 at 10:11
  • Assign / Add this user in site by site.
    – Ravi Patel
    Commented Mar 6, 2018 at 10:17
  • Thank You. It works. Assigning new "super admin" site by site. Commented Mar 6, 2018 at 11:13
0

I think you're looking for the core functions:

The recommended way to extend the functionality of WordPress, is to write plugins that use hook into actions and filters, instead of booting WordPress core in external PHP files.

There's also a setting for that:

enter image description here

when we edit each user in /wp-admin/network/users.php

3
  • I want to run PHP file directly from root folder. Is there any way to create super admin that can access all multi-site without creating plugin. ? Commented Mar 6, 2018 at 9:09
  • There's also a setting with a UI for that, when you edit a user from wp-admin/network/users.php
    – birgire
    Commented Mar 6, 2018 at 9:20
  • This option is checked for all newly created "super admin". But when i logged in with new "super admin" it will only allow to edit main site and not sub site. Commented Mar 6, 2018 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.