CSV to html

The link to the the latest version of WordPress plugin (Display a (html)table from one or more csv file), but I am using modified the old one (code is mess, but it works....)


  1. Only UTF-8 encoding is allowed. MS Office cannot create UTF-8 .csv file. Just use LibreOffice 🙂 It can.
  2. I do not use guessing about EOL, just use PHP built-in method to read/parse .csv
  3. Not all the customers are smart as you are, and word 'shortcode' could cause the heart attack. So let's make it simple. Added filter for content to create shortcode from the link to .csv file: just go to the media library, upload a file and add it to the post/page. The text between tags <a></a> will be displayed as a table name. See code below, added to functions.php file. The only problem right now that all tables will have the same classes: responsive, bordered, stripped, it's under html_class="" in the code. 99.99% is ok.
  4. First row is a list of headers, except, it will be ignored if it's empty/blank

add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );
function filter_the_content_in_the_main_loop( $content ) {
  // Check if we're inside the main loop in a single post page.
 if ( /* is_single() && */ in_the_loop() && is_main_query() ) { return preg_replace('~<p><a\s[^>]*href=\"([^\"]*)\.csv\"[^>]*>(.*)<\/a><\/p>~siU', '[csvtohtml-create source_type="mine" title="$2" html_class="table table-bordered table-striped" source_files="$1.csv"]', $content);
  }
  return $content;
}
Table with Headers
x 2x 3x 4Cream
Latisse 3ml7oz9 oz
Latisse 5 ml13 oz
Table without Headers
Latisse 3ml7ozn/an/an/a9 oz
Latisse 5 ml13 ozn/an/an/a

Source: CSV to html – WordPress plugin | WordPress.org

Leave a Reply

Your email address will not be published. Required fields are marked *

Yandex.Metrica