top of page

Export CSV from Custom Table in wordpress

  • Writer: سُلَيْمَان بْن دَاوُوْد
    سُلَيْمَان بْن دَاوُوْد
  • 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();
}






 
 
 

Recent Posts

See All
Duplicate Category in Wordpress

The following code helps to duplicate the default wordpress category by help of code. // Add Duplicate option for categories in WordPress...

 
 
 
C#.NET and Its Associated Codes

1- Creating an Desktop Application in C#.NET C# ADO.NET & MySQL MAIN C# and MySQL ADO.NET with Crystals Reports Ready Code 2- Handling...

 
 
 

Comments


© 2020 by syednazrulhassan

bottom of page