Copy user role on multisite so the user can access subsites with same role

You can use the below function to copy all the users to all the subsite. It’s actually getting all the user IDs and subsites IDs first, then it’s looping over every sites and users to assign all the users to every subsite. Here is the updated code-

add_action('wp','the_dramatist_add_current_user_to_site');
function the_dramatist_add_current_user_to_site() {
    global $wpdb;
    $all_users = $wpdb->get_col( 'SELECT ID FROM $wpdb->users' );
    $subsites = get_sites();
    foreach( $subsites as $subsite ) {
        $subsite_id = get_object_vars($subsite)['blog_id'];
        foreach ( $all_users as $current_user) {
            $blog_id = get_current_blog_id();
            $user_id = ($current_user->ID);

            switch_to_blog($subsite_id);
            $get_role = ($current_user->caps);
            $add_role = key($get_role);
            restore_current_blog();

            if (!is_user_member_of_blog( $user_id, $blog_id ) ) {
                add_user_to_blog( $blog_id, $user_id, $add_role );
            }
        }
    }
}

Hope that thing helps.