8月 282011
「Simple Colors::CSVファイルのデータからテーブルを表示させるショートコード」を発見!本ブログではあまり Table を利用しないのですが、もう一つのブログ「俺流!テニス論 2.0」では、頻繁に Table を利用しています。Table タグ・・・めんどくさいんですよね!
そこで、functions.php にするには大掛かりなコードなので参照の投稿記事をベースに独自プラグンを開発しました(といってもほとんどがコピペですが・・・)。
早速、CSV ファイルを作成、特別なタグを利用すると・・・以下がサンプルですが美しい~自分で感動しているのもどうかとは思いますが、それでも嬉しいものです。サンプルの下に全てのコードをアップしておきます。
| Year | Grade | Ranking | Points | Tours | Ave |
|---|---|---|---|---|---|
| 2011.07 | 高校3年生 | 217 | 148 | 16 | 9.25 |
| 2010.12 | 高校2年生 | 220 | 157 | 15 | 10.47 |
| 2009.12 | 高校1年生 | 303 | 109 | 12 | 9.08 |
| 2008.12 | 中学3年生 | 646 | 33 | 6 | 5.50 |
| 2007.12 | 中学2年生 | 659 | 30 | 4 | 7.50 |
| 2006.12 | 中学1年生 | 838 | 17 | 3 | 5.67 |
<?php
/*
Plugin Name: AAA-ZeroTable
Plugin URI: http://www.warna.info/archives/501/
Description: CSVファイルのデータからテーブルを表示させるプラグイン
Version: 1.0
Author: Zero Cool
Author URI: http://www.startat50.net/
*/
function table_shortcode( $atts ) {
$default = array(
'th' => 'row',
'caption' => '',
'file' => '',
'id' => false,
'charset' => 'sjis-win',
'table_id' => '',
'table_class' => '',
'tfoot' => 'false',
);
$args = shortcode_atts( $default, $atts );
if ( is_numeric( $args['id'] ) ) {
$post = get_post( $args['id'] );
if ( $post->post_mime_type == 'text/csv' ) {
$args['file'] = $post->guid;
}
}
$match_num = 0;
if ( preg_match( '|^https?://|', $args['file'] ) && strpos( $args['file'], '../' ) === false ) {
$file = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $args['file'], $match_num );
} elseif ( preg_match( '|^/|', $args['file'] ) && strpos( $args['file'], '../' ) === false ) {
$file = $_SERVER['DOCUMENT_ROOT'] . $args['file'];
$match_num = 1;
}
if ( $match_num && file_exists( $file ) && is_readable( $file ) ) {
$charset = in_array( strtolower( $args['charset'] ), array( 'utf-8', 'euc-jp', 'eucjp-win', 'sjis', 'sjis-win', 'iso-2022-jp' ) ) ? $args['charset'] : $default['charset'];
$th = in_array( strtolower( $args['th'] ), array( 'col', 'row', 'both' ) ) ? strtolower( $args['th'] ) : $default['th'];
$fh = fopen( $file, 'r' );
if ( $fh ) {
$row_num = $th == 'row' ? 1 : 0;
$table_id = $args['table_id'] ? ' id="' . esc_attr( $args['table_id'] ) . '"' : '';
$table_class = $args['table_class'] ? ' class="' . esc_attr( $args['table_class'] ) . '"' : '';
$output = '<table' . $table_id . $table_class . '>' . "n";
if ( $args['caption'] ) {
$output .= '<caption>' . esc_html( $args['caption'] ) . "</caption>n";
}
while ( $row = Ps_fgetcsv_reg( $fh, $args['charset'] ) ) {
if ( $row_num == 0 ) {
$row_class = 'head_row';
} else {
$row_class = $row_num % 2 ? 'odd' : 'even';
}
if ( $row_num == 0 ) {
$output .= '<thead>' . "n";
} elseif ( $row_num == 1 ) {
$output .= '<tbody>' . "n";
}
$line = '<tr class="' . $row_class . '">' . "n";
for ( $i = 0; $i < count( $row ); $i++ ) {
$elm = ( $th != 'col' && $i == 0 ) || $row_num == 0 ? 'th' : 'td';
$line .= '<' . $elm . '>' . $row[$i] . '</' . $elm . ">n";
}
$line .= '</tr>' . "n";
$output .= $line;
if ( $row_num == 0 ) {
$output .= '</thead>' . "n";
if ( in_array( strtolower( $args['tfoot'] ), array( '1', 'true' ) ) ) {
$output .= '<tfoot>' . "n";
$output .= '<tr class="' . $row_class . '">' . "n";
$output .= $line;
$output .= '</tr>' . "n";
$output .= '</tfoot>' . "n";
}
}
$row_num++;
}
$output .= "</tbody>n";
$output .= '</table>';
}
}
return $output;
}
add_shortcode( 'csv2table', 'table_shortcode' );
function Ps_fgetcsv_reg ( &$handle, $charset = 'sjis-win', $length = null, $d = ',', $e = '"' ) {
$d = preg_quote( $d );
$e = preg_quote( $e );
$_line = "";
$eof = false;
while ( ( $eof != true ) and ( ! feof( $handle ) ) ) {
$_line .= ( empty( $length ) ? fgets( $handle ) : fgets( $handle, $length ) );
$itemcnt = preg_match_all( '/'.$e.'/', $_line, $dummy );
if ( $itemcnt % 2 == 0 ) $eof = true;
}
if ( strtolower( $charset ) != 'utf-8' ) {
$_line = mb_convert_encoding( $_line, 'UTF-8', $charset );
}
$_csv_line = preg_replace( '/(?:rn|[rn])?$/', $d, trim( $_line ) );
$_csv_pattern = '/('.$e.'[^'.$e.']*(?:'.$e.$e.'[^'.$e.']*)*'.$e.'|[^'.$d.']*)'.$d.'/';
preg_match_all( $_csv_pattern, $_csv_line, $_csv_matches );
$_csv_data = $_csv_matches[1];
for( $_csv_i = 0; $_csv_i < count( $_csv_data ); $_csv_i++ ) {
$_csv_data[$_csv_i] = preg_replace( '/^'.$e.'(.*)'.$e.'$/s', '$1', $_csv_data[$_csv_i] );
$_csv_data[$_csv_i] = str_replace( $e.$e, $e, $_csv_data[$_csv_i] );
}
return empty( $_line ) ? false : $_csv_data;
}
?>
以下の投稿記事も興味がおありかも・・・
- WordPress の固定ページ一覧に順序項目を表示させるプラグイン(2011年08月19日)
- URL を記入するだけでウェブのスクリーンショットを撮るプラグイン(2011年08月20日)
- WordPress の functions.php を変更したらアクセスできなくなった!(2011年08月12日)
- テーマの functions.php を書き換えずに WP プラグインを開発!(2011年08月14日)
- すげぇ~ 素人でも WordPress 用プラグインが開発できた!(2011年08月14日)
- WordPress のプラグイン開発は素人でもできる!?(2011年08月13日)
- WordPress の標準エディターのフォントを変更するプラグイン(2011年08月30日)
- WordPress で会員制サイトを開発する!(1) 認証画面の設定(2011年08月30日)
- 絶対必要な WordPress 用 2 つのプラグイン(2011年06月05日)
- サーバー系の WordPress プラグン 2 つをインストール(2011年06月06日)


最近のコメント