<?php
/*
Plugin Name: MassageNet Export Users
Description: Adds a button to the admin panel to enable export specific user fields from the wp_usermeta and wp_user database tables to a CSV file. The export is restricted to administrator users (based on the 'export' capability) The specific fields are defined in the PHP code of the plugin. User fields are specific to MassageNet.org custom user fields and will not work without modification. In order to use this plugin, you will need to edit the code. If you don't know how and don't want to learn how, then find a programmer who does. If you want to use this plugin, you will need to mofify the code. My code is based on <a href="http://mikeschinkel.com">Mike Schinkel</a>'s <a href="http://wordpress.stackexchange.com/questions/3480/how-can-i-force-a-file-download-in-the-wordpress-backend">post</a> on Wordpress Stackexchange: http://goo.gl/wqNyZ with help from <a href="http://www.mattvarone.com/wordpress/list-users-with-wp_user_query/">sksmatt</a> and <a href="http://scribu.net/wordpress/the-magic-of-wp_user.html">scribu</a>. Thank you!
Version: 0.3
Author: James Carroll <jcarroll@massagenet.org>
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
*/
?>