Want more features on Pastebin? Sign Up, it's FREE!
Guest

MassageNet Export Users

By: jcarroll_massagenet on May 15th, 2012  |  syntax: PHP  |  size: 3.81 KB  |  views: 67  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Plugin Name: MassageNet Export Users
  4. 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!
  5. Version: 0.3
  6. Author: James Carroll <jcarroll@massagenet.org>
  7. Author URI: http://linkedin.com/in/jfcarroll
  8. License: Copyright 2012 James Carroll, released under MIT License
  9.  */
  10. if (!class_exists('MN_Export_Users')) {
  11.   class MN_Export_Users {
  12.        
  13.         static function on_load() {
  14.           add_action('plugins_loaded',array(__CLASS__,'plugins_loaded'));
  15.           add_action('admin_menu',array(__CLASS__,'admin_menu'));
  16.         }
  17.         static function admin_menu() {
  18.           add_submenu_page('users.php', // Parent Menu
  19.                 'Export as CSV',                                // Page Title
  20.                 'Export as CSV',                                // Menu Option Label
  21.                 'export',                               // Capability
  22.                 'users.php?export=massagenet_users.csv');// Option URL relative to /wp-admin/
  23.         }
  24.         static function plugins_loaded() {
  25.           global $pagenow;
  26.           if ($pagenow=='users.php' &&
  27.                 current_user_can('export') &&
  28.                 isset($_GET['export'])  &&
  29.                 $_GET['export']=='massagenet_users.csv') {
  30.                         $today = date('Y-m-d');
  31.                         $fp = fopen('php://temp', 'r+');
  32.                         MN_Export_Users::export_users_csv($fp);
  33.                         header("Content-type: application/csv");
  34.                         header("Content-Disposition: attachment; filename=massagenet_users-" . $today . ".csv");
  35.                         header("Pragma: no-cache");
  36.                         header("Expires: 0");
  37.                         rewind($fp);
  38.                         fpassthru($fp);
  39.                         fclose($fp);
  40.                         exit();
  41.           }
  42.        
  43.         }
  44.         // Arg: File handle (pointer)
  45.         static function export_users_csv($fp) {
  46.                 $wp_user_search  = new WP_User_Query( array( /*'role' => 'subscriber', */'fields' => 'all_with_meta' ) );
  47.                 $users = $wp_user_search->get_results();
  48.  
  49.                 $massagenet_user_fields = array(
  50.                         'research_id',
  51.                         //'ID', // First field cannot be 'ID' due to Excel parser bug, explained here: http://support.microsoft.com/kb/323626
  52.                         'user_login',
  53.                         'user_email',
  54.                         'display_name',
  55.                         /*'title',
  56.                         'first_name',
  57.                         'middle_initial',
  58.                         'last_name',
  59.                         'suffix',*/
  60.                         'city',
  61.                         'postal_code',
  62.                         'state_province',
  63.                         'country',
  64.                         'massage_therapist',
  65.                         'massage_researcher',
  66.                         'massage_educator',
  67.                         'massage_student',
  68.                         'user_type_other',
  69.                         'user_type_other_text',
  70.                         'graduation_date',
  71.                         'licensed',
  72.                         'practice_state',
  73.                         'license_expiration',
  74.                         'user_registered',
  75.                         'last_login',
  76.                         'referral_source'
  77.                 );
  78.                 $SEPARATOR = ',';
  79.  
  80.                 fputcsv($fp, $massagenet_user_fields);
  81.                 foreach ( $users as $user )
  82.                 {
  83.                         $line = array();
  84.                         foreach ($massagenet_user_fields as $field) {
  85.                                 $line[] .= $user->get( $field );
  86.                         }
  87.                         fputcsv($fp, $line);
  88.                 }
  89.                 return $fp;
  90.         }
  91.   }
  92.   MN_Export_Users::on_load();
  93. }
  94. /*
  95. Changelog
  96.  
  97. Version 0.3 - 15 May 2012
  98. * Posted to pastebin: http://pastebin.com/NkVv9bSn
  99. * No change to code
  100.  
  101. Version 0.2 - 30 Apr 2012
  102. * Changed wp-last-login to last_login
  103. * Rearranged columns
  104. * Added additional header information
  105.  
  106. Version 0.1 - Initial version
  107.  
  108. */
  109. ?>
clone this paste RAW Paste Data