Export WordPress page to CSV from front end

I’m looking for a WordPress plugin that takes a section of the page content – e.g. a price list – and exports it to CSV. So I can place a “Download CSV” link on the page that users can click.

If you know if a plugin that does this, or could be easily modified to do this, I’d greatly appreciate it.

Related posts

Leave a Reply

1 comment

  1. Here’s the code I wrote on the off chance it helps someone else:

    add_action('wp', 'generate_csv');
    function generate_csv()
    {
        if ('csv' == (isset($_GET['format']) ? $_GET['format'] : null)) {
            $calendar_shortcode_id = get_field("calendar_shortcode_id");
            if($calendar_shortcode_id) {
                global $post;
                $shortcode = "[google-calendar-events id='$calendar_shortcode_id' type='list']";
                $html = do_shortcode($shortcode);
                $doc = new DOMDocument(); $doc->loadHTML($html);
                $titles = array();
                $table=$doc;
                $a=0;
                $delim = "";
                $csv="";
                $num_cols=5;
                foreach($table->getElementsByTagName('span') as $td) {
                if($a==0) {$delim = "";}
                if($a<$num_cols) { $csv .= $delim . '"' . $td->nodeValue. '"';
                $delim = ",";
                }
                if($a==($num_cols-1)) { $csv .= "n";$a=-1;}
                $a++;
                }
                header('Content-Encoding: UTF-8');
                header("Content-type: text/csv; charset=utf-8");
                header("Content-Disposition: attachment; filename=".$post->post_name.".csv");
                echo "xEFxBBxBF"; // UTF-8 BOM
                echo utf8_decode($csv);
                exit;
            }
        }
    }