Halloween Sale 15% off Ends in See the plans
PHP

Export Network Users to CSV

Adds an "Export CSV" button to Network Admin > Users that downloads every network user with their email, registration date, number of sites and super admin status.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! is_multisite() ) {
    return;
}

add_action( 'network_admin_notices', function () {
    $screen = get_current_screen();

    if ( ! $screen || 'users-network' !== $screen->id || ! current_user_can( 'manage_network_users' ) ) {
        return;
    }

    printf(
        '<div class="notice notice-info"><p><a class="button" href="%1$s">%2$s</a></p></div>',
        esc_url( wp_nonce_url( network_admin_url( 'edit.php?action=wpide_export_users' ), 'wpide_export_users' ) ),
        esc_html__( 'Export all users as CSV' )
    );
} );

add_action( 'network_admin_edit_wpide_export_users', function () {
    check_admin_referer( 'wpide_export_users' );

    if ( ! current_user_can( 'manage_network_users' ) ) {
        wp_die( esc_html__( 'Sorry, you are not allowed to export users.' ), 403 );
    }

    nocache_headers();
    header( 'Content-Type: text/csv; charset=utf-8' );
    header( 'Content-Disposition: attachment; filename=network-users-' . gmdate( 'Y-m-d' ) . '.csv' );

    $out    = fopen( 'php://output', 'w' );
    $supers = get_super_admins();
    fputcsv( $out, array( 'ID', 'Username', 'Email', 'Display name', 'Registered', 'Sites', 'Super admin' ) );

    $paged = 1;
    do {
        $users = get_users( array( 'blog_id' => 0, 'number' => 500, 'paged' => $paged, 'orderby' => 'ID', 'fields' => array( 'ID', 'user_login', 'user_email', 'display_name', 'user_registered' ) ) );
        foreach ( $users as $user ) {
            $row = array( $user->ID, $user->user_login, $user->user_email, $user->display_name, $user->user_registered, count( get_blogs_of_user( $user->ID ) ), in_array( $user->user_login, $supers, true ) ? 'yes' : 'no' );
            fputcsv( $out, array_map( function ( $cell ) {
                $cell = (string) $cell;
                return preg_match( '/^[=+\-@]/', $cell ) ? "'" . $cell : $cell; // Avoid spreadsheet formula injection.
            }, $row ) );
        }
        $paged++;
    } while ( count( $users ) === 500 );

    fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions
    exit;
} );

Notes

Protected by a nonce and the manage_network_users capability. Very large networks are exported in batches of 500 users to keep memory low.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.