WPMU: Add User to All Sites in a Multisite  Network

Programmatically add a user to all sites in a WordPress multisite network (WPMU)

One of the pesky things about WordPress MU is testing. Every time I work with a WPMU site I find myself needing single level site admins in addition to network super admin users. Multisite provides an Add User screen inside of the Network Admin area to do this manually, but it’s slow and cumbersome. I’ve written the following function to automate the process and handle the heavy lifting for me dynamically.

How It Works

The general idea is to add a single user by email to all sites in your network. To do this we need to grab a list of sites, find the existing user by email, then add it to each site one by one.

Listed out as steps the process looks like this:

  1. Visit your multisite network with a query parameter in the URL: [email protected]. The email address set as the value will be the user you want
  2. Verify a user exists for the email provided in the GET parameter
  3. Get a list of all sites in the network
  4. Add this user with the same role to all sites in the network, one by one

I’ve specifically set this up to default to an administrator role, which you probably don’t want to do. I do this because I typically use the approach to add a super admin user to each site as a site-level administrator. It works for me, but may pose a security concern for you if you’re not careful or don’t fully know what you’re doing.

Function to Add User to All WPMU Sites

Add the following function to your WordPress codebase, either in your active theme or a plugin.

/**
 * WPMU Add User to All Sites
 *
 * Add an administrator user to all sites in a WordPress multisite network
 */
function kevinlearynet_wpmu_add_user_all_sites() {
  $email = isset( $_GET['wpmu_admin_sync'] ) ? esc_attr( $_GET['wpmu_admin_sync'] ) : null;
  if ( ! $email ) {
    return;
  }

  $user = get_user_by( 'email', $email );
  if ( $user === false ) {
    wp_die( "No user exists in this network with the email $email." );
  }

  $role = isset( $user->roles[0] ) ? $user->roles[0] : 'administrator';

  $sites = get_sites( [
    'limit' => 0,
    'public' => true,
    'spam' => false,
    'deleted' => false,
    'archived' => false,
    'mature' => false,
  ] );

  $output = [];
  foreach ( $sites as $site ) {
    $site_id = get_object_vars( $site )['blog_id'];
    $site_name = get_blog_details( $site_id )->blogname;
    $add_user = add_user_to_blog( $site_id, $user->ID, $role );
    $note = "$email user to $site_name as an $role.";
    $output[] = $add_user ? "Successfully added $note" : "Failed adding $note";
  }
  $html = wpautop( implode( PHP_EOL, $output ) );
  wp_die( $html );
}
add_action( 'after_setup_theme', 'kevinlearynet_wpmu_add_user_all_sites' );

Once added you’ll be able to run the process on-demand using the following URL query: [email protected].

Results

To run the process visit your WPMU site and add [email protected] onto the end of the URL, replacing [email protected] with the email address of the user you want to add to every site in your MU network. When the process completes a report will be shown displaying the results.

That’s it! This makes an otherwise slow and cumbersome process extremely quick and painless, I hope it help’s you avoid some frustration. Happy coding!

Related Articles

Meet the Author

Kevin Leary, WordPress Consultant

I'm a custom WordPress web developer and analytics consultant in Boston, MA with 17 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.