export to csv wordpress

前端 未结 5 487
后悔当初
后悔当初 2020-12-24 04:13

I need to export data in one table in a csv file. I\'m able to get the data fine but the CSV file is not being generated by the browser.

My code is like this: its th

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-24 04:37

    This is working perfectly now. we can use this as a plugin. I modified this post. thanks to sruthi sri.

    Hope this helps some one :)

    generate_csv();
    
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"report.csv\";" );
    header("Content-Transfer-Encoding: binary");
    
    echo $csv;
    exit;
    }
    
    // Add extra menu items for admins
    add_action('admin_menu', array($this, 'admin_menu'));
    
    // Create end-points
    add_filter('query_vars', array($this, 'query_vars'));
    add_action('parse_request', array($this, 'parse_request'));
    }
    
    /**
    * Add extra menu items for admins
    */
    public function admin_menu()
    {
    add_menu_page('Download Report', 'Download Report', 'manage_options', 'download_report', array($this, 'download_report'));
    }
    
    /**
    * Allow for custom query variables
    */
    public function query_vars($query_vars)
    {
    $query_vars[] = 'download_report';
    return $query_vars;
    }
    
    /**
    * Parse the request
    */
    public function parse_request(&$wp)
    {
    if(array_key_exists('download_report', $wp->query_vars))
    {
    $this->download_report();
    exit;
    }
    }
    
    /**
    * Download report
    */
    public function download_report()
    {
    echo '
    '; echo '
    '; echo '

    Download Report

    '; //$url = site_url(); echo '

    Export the Subscribers'; } /** * Converting data to CSV */ public function generate_csv() { $csv_output = ''; $table = 'users'; $result = mysql_query("SHOW COLUMNS FROM ".$table.""); $i = 0; if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { $csv_output = $csv_output . $row['Field'].","; $i++; } } $csv_output .= "\n"; $values = mysql_query("SELECT * FROM ".$table.""); while ($rowr = mysql_fetch_row($values)) { for ($j=0;$j<$i;$j++) { $csv_output .= $rowr[$j].","; } $csv_output .= "\n"; } return $csv_output; } } // Instantiate a singleton of this plugin $csvExport = new CSVExport();

提交回复
热议问题