CSSタグの投稿


WordPress 2.7 のカテゴリー/タグごと CSS 割り当て機能を修正する
新機能の問題点を指摘していた WordPress 2.7 ですが、さきほどリリースされてしまいました。trac のチケットを切ったり、WordPress 日本語フォーラムでトピックを作ったんですが、結局直されませんでした……。やっぱり「WordPress 開発者の I18N 対応は優先度が低い」と言えます。これらの改善のためには、英語以外のユーザーが増えてもっと口出ししなければなりません。
さて、嘆いていても進まないので、とりあえず改善用プラグインを作ってみました。詳細は日本語フォーラムを見てもらうとして、ここでは改善コードを出しておきましょう (スラッグを URL デコードする、という仕組みです)。以下のコードを新規テキストファイルにコピー & ペーストして適当な名前 (fix-post_class.php など) で保存し、WordPress のプラグインディレクトリーにアップロードすれば OK です。WordPress 2.7 では、zip ファイルにしておけばインストールが簡単なんですが、「あえて敷居を高くする」ということで、このままにしておきます
<?php /* Plugin Name: Fix Post Class Plugin URI: http://www.yuriko.net/arc/2008/12/11/fix-post_class/ Description: Use url-decoded CSS class names for assigned categories/tags of each post with WordPress 2.7 Version: 1.0.0 Author: IKEDA yuriko Author URI: http://www.yuriko.net/cat/wordpress/ */ /* Copyright (c) 2008 yuriko This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ function fix_post_class($classes, $class, $post_id) { $num_classes = count($classes); if ($num_classes < 1) { return $classes; } for ($i = 0 ; $i < $num_classes ; $i++) { if (strpos($classes[$i], '%') !== 0) { $classes[$i] = preg_replace('/[^-_0-9a-zA-Z\x80-\xff]/', '', urldecode($classes[$i])); } } return $classes; } add_filter('post_class', 'fix_post_class', 99, 3); ?>
[追記] 日本語フォーラムでの議論では、XHTML 1.0 の場合は「WordPress 2.7 の実装は問題ないが、style.css の方では % 記号をバックスラッシュでエスケープする必要がある」「上記プラグインによる Unicode 化も正しい方法である」(可読性ではこちらが上) となります。しかし、XHTML 1.1 では、% 記号に加え、Unicode 文字列が不可となるため、WordPress の実装自体 NG となります (上記プラグインの解決方法もだめ)。XHTML 1.1 は class 属性の部分が NMTOKENS になったのですが、NMTOKES は英数字だけじゃなくて Unicode の文字列も含まれるため、日本語でも OK です。つまり、上記プラグインは XHTML 1.1 でも使えます。post_class()
関数は、XHTML 1.1 の出力かどうかで動作を変える必要があるわけです。上記プラグインは、XHTML 1.0 でのみ使ってください。