Created
March 5, 2022 09:56
-
-
Save kingjmaningo/d512b7a6685dfbe79975d819ace13f13 to your computer and use it in GitHub Desktop.
Wordpress export custom data to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (isset($_GET['download-users'])) { | |
global $wpdb; | |
// Fetch data from a sepecific table(wp_users) from database | |
$query = $wpdb->get_results("SELECT * FROM `wp_users`"); | |
$filename = "wp_users_" . date('Y-m-d') . ".csv"; | |
$path = wp_upload_dir(); // or where ever you want the file to go | |
$outstream = fopen( 'php://output', 'w' ); | |
$user_fields_csv = array('Row','ID','user_login','user_pass','user_email','user_registered'); | |
ob_end_clean(); | |
header( "Content-Disposition: attachment; filename={$filename}" ); | |
fputcsv( $outstream, $user_fields_csv ); | |
foreach ($query as $key => $value) { | |
$lineData = array($key, $value->ID, $value->user_login, $value->user_pass, $value->user_email, $value->user_registered); | |
fputcsv($outstream, $lineData); | |
} | |
exit(); | |
} | |
?> | |
<a href="/?download-users">Download users</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment