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.



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' );

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

Leave a comment