Skip to content

Instantly share code, notes, and snippets.

@maxkostinevich
Last active February 11, 2023 20:03
Show Gist options
  • Save maxkostinevich/dbcb07f36ad4276c1010 to your computer and use it in GitHub Desktop.
Save maxkostinevich/dbcb07f36ad4276c1010 to your computer and use it in GitHub Desktop.
WordPress: Export custom data to CSV
<?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();
}
@djcowan
Copy link

djcowan commented Apr 29, 2020

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