-
-
Save maxkostinevich/dbcb07f36ad4276c1010 to your computer and use it in GitHub Desktop.
<?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(); | |
} |
This is coreect code of file export in proper way please try it!
if (isset($_POST["export"]))
{
$filename = 'Student_Table_' . time() . '.csv';
$header_row = array(
'S NO.',
'First Name',
'Last Name',
'Email',
'parent Name',
'Address'
);
$data_rows = array();
$users = $wpdb->get_results("SELECT * FROM ".$table_prefix."student ","ARRAY_A");
foreach ( $users as $user )
{
$row = array(
$user['id'],
$user['f_name'],
$user['l_name'],
$user['email'],
$user['p_name'],
$user['address']
);
$data_rows[] = $row;
}
ob_end_clean ();
$fh = @fopen( 'php://output', 'w' );
header( "Content-Disposition: attachment; filename={$filename}" );
fputcsv( $fh, $header_row );
foreach ( $data_rows as $data_row )
{
fputcsv( $fh, $data_row );
}
exit();
}
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?
Thanks @maxkostinevich:
public function perfect( "first-time" ) : WP_Nice_One { return $this->is_much_appreciated(); }
Hello. You have to add ob_end_clean() before $fh = @fopen( 'php://output', 'w' );
fclose($fh); exit();