
WordPressで表組みを表示・更新するのは、HTMLが分かる人ならなんとでもなりますが、そうでない人が担当するとなると・・・結構困ったことになりますね。
そんなわけで、CSVファイルをアップロードして、テーブルとして表示できるショートコードを作成してみました。
利用方法
1.CODE 1 をテーマのfunctions.phpに追記するか、独自のプラグインにして有効化してください。
2.メディアアップローダーから表として表示するCSVファイルをアップロードします。

3.テーブルを表示させたい箇所に csv2table ショートコードを記述してください。最低限必要なパラメータは、メディアのidか、ファイルの URLになります。


その他、下記パラメータにて、出力を変更することができます。
パラメータ
- th
- thとして表示するセルの指定。row:左列、col:上部、both:左列および最上列。デフォルトはrow
- caption
- キャプションとして出力するテキスト。デフォルトは出力なし
- file
- アップロードした際に表示されるCSVファイルのURLデフォルトはなし。idかfileパラメータが必須
- id
- アップロードしたCSVファイルのid。idかfileパラメータが必須
- charset
- CSVファイルの文字コード。デフォルトはsjis-win(Windows Shift-JIS)
- table_id
- table要素のid属性。デフォルトはなし
- table_class
- table要素のclass属性。デフォルトはなし
- tfoot
- tfootを表示するかどうか。1で表示。デフォルトは表示しない
書式サンプル
[csv2table id="30" table_id="sample-table"]
CODE 1
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( '/(?:\r\n|[\r\n])?$/', $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;
}
こんなプラグインも
管理画面で編集も出来る WP-Table Reloaded というのもありました。むむ、良くできている。
CSVからのインポートについては、当然ながらShift-JISなどサポートしているわけではないでしょうから、UTF-8に変換してからインポートしてあげれば良いでしょう。
「CSVファイルのデータからテーブルを表示させるショートコード」への4件のフィードバック