Mike Schinkel's post on Wordpress Stackexchange: http://goo.gl/wqNyZ with help from sksmatt and scribu. Thank you!
Version: 0.3
Author: James Carroll
Author URI: http://linkedin.com/in/jfcarroll
License: Copyright 2012 James Carroll, released under MIT License
*/
if (!class_exists('MN_Export_Users')) {
class MN_Export_Users {
static function on_load() {
add_action('plugins_loaded',array(__CLASS__,'plugins_loaded'));
add_action('admin_menu',array(__CLASS__,'admin_menu'));
}
static function admin_menu() {
add_submenu_page('users.php', // Parent Menu
'Export as CSV', // Page Title
'Export as CSV', // Menu Option Label
'export', // Capability
'users.php?export=massagenet_users.csv');// Option URL relative to /wp-admin/
}
static function plugins_loaded() {
global $pagenow;
if ($pagenow=='users.php' &&
current_user_can('export') &&
isset($_GET['export']) &&
$_GET['export']=='massagenet_users.csv') {
$today = date('Y-m-d');
$fp = fopen('php://temp', 'r+');
MN_Export_Users::export_users_csv($fp);
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=massagenet_users-" . $today . ".csv");
header("Pragma: no-cache");
header("Expires: 0");
rewind($fp);
fpassthru($fp);
fclose($fp);
exit();
}
}
// Arg: File handle (pointer)
static function export_users_csv($fp) {
$wp_user_search = new WP_User_Query( array( /*'role' => 'subscriber', */'fields' => 'all_with_meta' ) );
$users = $wp_user_search->get_results();
$massagenet_user_fields = array(
'research_id',
//'ID', // First field cannot be 'ID' due to Excel parser bug, explained here: http://support.microsoft.com/kb/323626
'user_login',
'user_email',
'display_name',
/*'title',
'first_name',
'middle_initial',
'last_name',
'suffix',*/
'city',
'postal_code',
'state_province',
'country',
'massage_therapist',
'massage_researcher',
'massage_educator',
'massage_student',
'user_type_other',
'user_type_other_text',
'graduation_date',
'licensed',
'practice_state',
'license_expiration',
'user_registered',
'last_login',
'referral_source'
);
$SEPARATOR = ',';
fputcsv($fp, $massagenet_user_fields);
foreach ( $users as $user )
{
$line = array();
foreach ($massagenet_user_fields as $field) {
$line[] .= $user->get( $field );
}
fputcsv($fp, $line);
}
return $fp;
}
}
MN_Export_Users::on_load();
}
/*
Changelog
Version 0.3 - 15 May 2012
* Posted to pastebin: http://pastebin.com/NkVv9bSn
* No change to code
Version 0.2 - 30 Apr 2012
* Changed wp-last-login to last_login
* Rearranged columns
* Added additional header information
Version 0.1 - Initial version
*/
?>