'users' != $current_screen->id
in your file $current_screen
is not set therefore, you are getting warning.
You can pass custom var in your user query and check before running your code.
$blogusers = get_users( 'orderby=nicename&order=ASC&my_filter=1' );
Here we are passing my_filter=1
then later we can check in pre_user_query
global $wpdb, $current_screen;
$vars = $user_search->query_vars;
if (isset($current_screen->id) && 'users' != $current_screen->id) {
return;
} else if (empty($vars['myfilter'])) {
return;
}
However, loading WordPress in a external file is not a good thing. Instead of external file you can wrap the whole code in a function and hook it on some action where headers are not sent.
Example:
Create a form instead of link e.g.
<form action="" method="POST">
<input type="hidden" name="myprefix_export_csv" value="1" />
<input type="submit" value="download" />
</form>
Then check if your custom key is set, if it is then export CSV with your current code.
add_action('init', 'export_csv'); //you can use admin_init as well
function export_csv() {
if (!empty($_POST['myprefix_export_csv'])) {
//Send headers
//Query users
//print data
exit();
}
}
You can also set nonce in form if you think other users can tweak it.