WordPress.org

Ready to get started?Download WordPress

Forums

CSV to SortTable
[resolved] Set the Column names independently (3 posts)

  1. gmackey
    Member
    Posted 1 year ago #

    I have used your plugin in the past and its great.
    Currently I am getting a DB dump as a CSV. The file has no header row with column names. Is there a way to set the Column names independently?

    http://wordpress.org/extend/plugins/csv-to-sorttable/

  2. Shaun Scovil
    Member
    Plugin Author

    Posted 1 year ago #

    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

  3. Shaun Scovil
    Member
    Plugin Author

    Posted 1 year ago #

    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!)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.