Export CSV from Custom Table in wordpress
- سُلَيْمَان بْن دَاوُوْد

- Jun 5, 2020
- 1 min read
function exportleads_settings(){ add_submenu_page( 'options-general.php', __( 'Export Popup Leads', 'consumeracquision' ), __( 'Export Popup Leads', 'consumeracquision' ), 'manage_options', 'export-popup-feeds', 'exportleads_callback' );}add_action('admin_menu', 'exportleads_settings'); function exportleads_callback() { ?> <div class="wrap"> <h2><?php _e( 'Export Popup Leads', 'textdomain' ); ?></h2> <a href="<?php echo admin_url(); ?>admin-post.php?action=our_action_hook">Export Popup Leads</a> </div> <?php}add_action( 'admin_post_our_action_hook', 'am_our_action_hook_function' );function am_our_action_hook_function() { if ( ! current_user_can( 'manage_options' ) ) return; $csv_filename = 'Leads_'.date('D M d Y H:i:s A e',time()).'.csv' ; header('Content-Type: application/csv'); header('Content-Disposition: attachment; filename='.$csv_filename); header('Pragma: no-cache'); header('Expires: 0'); $file = fopen('php://output', 'w'); // send the column headers fputcsv($file, array('S.No','Email Address'),','); global $wpdb; $sql = 'SELECT * FROM '.$wpdb->prefix.'leads'; $users = $wpdb->get_results( $sql, 'ARRAY_A' ); $i=1; foreach ( $users as $user ) { fputcsv($file, array($i, $user['email'] ) ); $i++; } exit();}




Comments