X

Shortcode tables with multiple rows and columns

Yet Another Related Posts Plugin Review

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.


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>';
    endforeach;
    $output .= '</tr><tr>';
    $counter = 1;
    foreach($data as $datum):
        $output .= '<td>'.$datum.'</td>';
        if($counter%$total==0):
            $output .= '</tr>';
        endif;
        $counter++;
    endforeach;
        $output .= '</table>';
    return $output;
}
add_shortcode( 'table', 'simple_table' );
[table “” not found /]

Comments  Leave a Reply

  1. Hi guys,

    This is a great snippet to easily create simple tables. I know the article is a little dated but I see Alex comment is 4 months old so my comment could be still useful to somene..

    The Snippet should be slightly modified declaring $output = ”; just before
    extract( shortcode_atts( array(
    Full shortcode function below.


    function simple_table( $atts ) {
    $output = '';
    extract( shortcode_atts( array(
    'cols' => 'none',
    'data' => 'none',
    ), $atts ) );
    $cols = explode(',',$cols);
    $data = explode(',',$data);
    $total = count($cols);
    $output .= '';
    foreach($cols as $col):
    $output .= ''.$col.'';
    endforeach;
    $output .= '';
    $counter = 1;
    foreach($data as $datum):
    $output .= ''.$datum.'';
    if($counter%$total==0):
    $output .= '';
    endif;
    $counter++;
    endforeach;
    $output .= '';
    return $output;
    }
    add_shortcode( 'table', 'simple_table' );

  2. Hi guys,

    when I add the code to my functions.php the parameters of [table cols="names,values" data="name1,25,name2,409"] do not get passed and it outputs none, none. You know why that is?

    Thanks

  3. Wordpress Table Shortcode Builder › Jean Egan June 30, 2012 at 1:38 am

    […] because I found this post by Kevin Chard. However, I encountered results that weren’t quite […]

  4. 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.
    Anne
     

    1. Change {$col} to ‘. $col . ‘ same for $datum

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!