Skip to content

Instantly share code, notes, and snippets.

@Otto42
Created January 3, 2016 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Otto42/10cba52d13667563002a to your computer and use it in GitHub Desktop.
Save Otto42/10cba52d13667563002a to your computer and use it in GitHub Desktop.
Simple tables plugin (use [table]...[/table] shortcode around CSV data in rows.
<?php
/*
Plugin Name: Simple Table
Plugin URI: http://ottopress.com/wordpress-plugins/simple-table/
Description: Add a [table] shortcode for making tables
Version: 0.1
Author: Otto
Author URI: http://ottopress.com
License: GPLv2
License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
*/
function simple_table_pre_process_shortcode($content) {
global $shortcode_tags;
$orig_shortcode_tags = $shortcode_tags;
$shortcode_tags = array();
add_shortcode('table','simple_table_shortcode');
$content = do_shortcode($content);
$shortcode_tags = $orig_shortcode_tags;
return $content;
}
add_filter('the_content', 'simple_table_pre_process_shortcode', 7);
function simple_table_shortcode( $atts, $content = "" ) {
$content = trim($content);
$rows = str_getcsv($content, "\n");
foreach($rows as $num => $row) {
$data[$num] = str_getcsv($row);
}
if ( !empty( $data ) ) {
$content = '<table>';
foreach ( $data as $row ) {
$content .= '<tr>';
foreach ( $row as $item ) {
$content .= '<td>'.$item.'</td>';
}
$content .= '</tr>';
}
$content .= '</table>';
}
return $content;
}
add_filter( 'no_texturize_shortcodes', 'shortcodes_to_exempt_from_wptexturize' );
function shortcodes_to_exempt_from_wptexturize( $shortcodes ) {
$shortcodes[] = 'table';
return $shortcodes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment