If you want to export all registered users in CSV format from your WordPress site, then this tutorial specially designed for you. Keep your close attention in this tutorial as I am going to share how to export all users as CSV in WordPress site.
In this tutorial, I will WordPress snippet you can use it within your wp-content/themes/your-theme-folder/functions.php file OR you can create a separate WordPress plugin using the below code.
You can customize the below code as per your requirement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | <?php class CSVExport { /** * Constructor */ public function __construct() { if(isset($_GET['download_report'])) { $csv = $this->generate_csv(); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private", false); header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"report.csv\";" ); header("Content-Transfer-Encoding: binary"); echo $csv; exit; } // Add extra menu items for admins add_action('admin_menu', array($this, 'admin_menu')); // Create end-points add_filter('query_vars', array($this, 'query_vars')); add_action('parse_request', array($this, 'parse_request')); } /** * Add extra menu items for admins */ public function admin_menu() { add_menu_page('Download Report', 'Download Report', 'manage_options', 'download_report', array($this, 'download_report')); } /** * Allow for custom query variables */ public function query_vars($query_vars) { $query_vars[] = 'download_report'; return $query_vars; } /** * Parse the request */ public function parse_request(&$wp) { if(array_key_exists('download_report', $wp->query_vars)) { $this->download_report(); exit; } } /** * Download report */ public function download_report() { echo '<div class="wrap">'; echo '<div id="icon-tools" class="icon32"> </div>'; echo '<h2>Download Report</h2>'; //$url = site_url(); echo '<p>Export the Subscribers'; } /** * Converting data to CSV */ public function generate_csv() { $csv_output = ''; $table = 'users'; $result = mysql_query("SHOW COLUMNS FROM ".$table.""); $i = 0; if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { $csv_output = $csv_output . $row['Field'].","; $i++; } } $csv_output .= "\n"; $values = mysql_query("SELECT * FROM ".$table.""); while ($rowr = mysql_fetch_row($values)) { for ($j=0;$j<$i;$j++) { $csv_output .= $rowr[$j].","; } $csv_output .= "\n"; } return $csv_output; } } // Instantiate a singleton of this plugin $csvExport = new CSVExport(); ?> |
Liked this tutorial? Do Like & share with your friends 🙂
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: export wordpress user in csv, wordpress csv export data, wordpress tutorial, wordpress tutorial for beginner, wordpress tutorial for beginners, wordpress tutorials, wordpress tutorials for beginners, wordpress user data, wordpress user export