Support » Plugin: CSV to SortTable » Set the Column names independently

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Shaun Scovil

    (@sscovil)

    Yes, there is!

    However, it involves adding a function to your functions.php file — or creating a separate plugin (if you want the functionality to persist after changing themes). This is because the plugin has no settings page, and doing that via shortcode parameters would be pretty messy.

    Here is a code example of how you would add a row to the CSV data array before it is parsed into an HTML table: https://gist.github.com/sscovil/5919849

    If you want to make it a plugin, you could do something like this: https://gist.github.com/sscovil/5919960

    Plugin Author Shaun Scovil

    (@sscovil)

    Keep in mind that both of these examples use column header names that are hard-coded and would be applied to ALL tables created by this plugin.

    You can add conditional tags to make sure these headers are only applied on certain pages or posts, like this:

    function csv_to_sorttable_add_header_row( $data ) {
        if ( is_page( 123 ) ) {
            $header_row = array( 'Column 1', 'Column 2', 'Column 3' );
            array_unshift( $data, $header_row );
        } elseif ( is_post( 'my-csv-data' ) {
            $header_row = array( 'Column A', 'Column B', 'Column C' );
            array_unshift( $data, $header_row );
        }
        return $data;
    }
    add_filter( 'csv_to_sorttable_data_array', 'csv_to_sorttable_add_header_row' );

    Likewise, you could program in some dynamic table header names using custom fields, like this:

    function csv_to_sorttable_add_header_row( $data ) {
        global $post;
        $cols = get_post_meta( $post->ID, 'csv-column-header' );
        $header_row = array( $cols[0], $cols[1], $cols[2] );
        array_unshift( $data, $header_row );
        return $data;
    }
    add_filter( 'csv_to_sorttable_data_array', 'csv_to_sorttable_add_header_row' );

    That example would grab the custom field values for csv-column-header from the current post/page, and use the first three values as column headings. (EDIT: You would probably want to check if those values were set, and have some default values if they are not!)

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Set the Column names independently’ is closed to new replies.