Last active
February 11, 2023 20:03
-
-
Save maxkostinevich/dbcb07f36ad4276c1010 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 | |
/** | |
* Export Data to CSV file | |
* Could be used in WordPress plugin or theme | |
*/ | |
// A sample link to Download CSV, could be placed somewhere in plugin settings page | |
?> | |
<a href="<?php echo admin_url( 'admin.php?page=myplugin-settings-page' ) ?>&action=download_csv&_wpnonce=<?php echo wp_create_nonce( 'download_csv' )?>" class="page-title-action"><?php _e('Export to CSV','my-plugin-slug');?></a> | |
<?php | |
// Add action hook only if action=download_csv | |
if ( isset($_GET['action'] ) && $_GET['action'] == 'download_csv' ) { | |
// Handle CSV Export | |
add_action( 'admin_init', 'csv_export' ; | |
} | |
function csv_export() { | |
// Check for current user privileges | |
if( !current_user_can( 'manage_options' ) ){ return false; } | |
// Check if we are in WP-Admin | |
if( !is_admin() ){ return false; } | |
// Nonce Check | |
$nonce = isset( $_GET['_wpnonce'] ) ? $_GET['_wpnonce'] : ''; | |
if ( ! wp_verify_nonce( $nonce, 'download_csv' ) ) { | |
die( 'Security check error' ); | |
} | |
ob_start(); | |
$domain = $_SERVER['SERVER_NAME']; | |
$filename = 'users-' . $domain . '-' . time() . '.csv'; | |
$header_row = array( | |
'Email', | |
'Name' | |
); | |
$data_rows = array(); | |
global $wpdb; | |
$sql = 'SELECT * FROM ' . $wpdb->users; | |
$users = $wpdb->get_results( $sql, 'ARRAY_A' ); | |
foreach ( $users as $user ) { | |
$row = array( | |
$user['user_email'], | |
$user['user_name'] | |
); | |
$data_rows[] = $row; | |
} | |
$fh = @fopen( 'php://output', 'w' ); | |
fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) ); | |
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' ); | |
header( 'Content-Description: File Transfer' ); | |
header( 'Content-type: text/csv' ); | |
header( "Content-Disposition: attachment; filename={$filename}" ); | |
header( 'Expires: 0' ); | |
header( 'Pragma: public' ); | |
fputcsv( $fh, $header_row ); | |
foreach ( $data_rows as $data_row ) { | |
fputcsv( $fh, $data_row ); | |
} | |
fclose( $fh ); | |
ob_end_flush(); | |
die(); | |
} |
Thanks @maxkostinevich:
public function perfect( "first-time" ) : WP_Nice_One { return $this->is_much_appreciated(); }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have made the corrections that others mentioned in the comments, and it works fine for me in the admin area on my plugin settings page. Awesome! Thank you.
How do I make this work on the front end? I have made the following changes and I'm still having issues with the link, which takes me to a Page Not Found when I click on it:
add_action( 'init', 'csv_export_storelist');
if( is_admin() ){ return false; }
<a href="<?php echo $_GET['uri'] ?>&action=download_csv_bc&_wpnonce=<?php echo wp_create_nonce( 'download_csv' )?>" class="export_mapdatafile">Export to CSV</a>
Any suggestions?