Created
January 3, 2016 14:44
-
-
Save Otto42/10cba52d13667563002a to your computer and use it in GitHub Desktop.
Simple tables plugin (use [table]...[/table] shortcode around CSV data in rows.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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