Home / admin / Shortcode tables with multiple rows and columns
  • How to Shortcode tables with multiple rows and columns


    Have you have ever wanted to quickly and easily create a simple dataset table? Well adding this snippet to the functions.php of your wordpress theme to enable table shortcodes. Bellow you can see the example shortcode that you can add multiple rows and columns of data separating each row and column with a comma. Thanks to Jean Egan who noticed an issue with the old version.

    wordpress snippet : PHP - functions.php

    function simple_table( $atts ) {
        extract( shortcode_atts( array(
            'cols' => 'none',
            'data' => 'none',
        ), $atts ) );
        $cols = explode(',',$cols);
        $data = explode(',',$data);
        $total = count($cols);
        $output .= '<table><tr class="th">';
        foreach($cols as $col):
            $output .= '<td>'.$col.'</td>';
        $output .= '</tr><tr>';
        $counter = 1;
        foreach($data as $datum):
            $output .= '<td>'.$datum.'</td>';
                $output .= '</tr>';
            $output .= '</table>';
        return $output;
    add_shortcode( 'table', 'simple_table' );

    wordpress snippet : SHORTCODE

    [table cols="names,values" data="name1,25,name2,409"]

    ( WordPress codex functions, hooks, in this snippet. )

    add_shortcode, shortcode_atts,
  • Cmonmail21

    Hie Kevin,
    Thanks for tis code.
    I have a problem, when inserting shortcode in post, it display variables ($x) and not value of variables.
    Thanks for help and sory for my bad english.

    • http://braginteractive.com Brad Williams

      Change¬†{$col} to ‘. $col . ‘ same for $datum

  • Pingback: Wordpress Table Shortcode Builder › Jean Egan()