WordPressのslugをectoから楽に設定できるようにするためのパッチ

自分はectoでブログを書いています。WordPressのデフォルトのサイテーな編集画面で文章を書かなくていいのと、比較的楽に画像をアップロードできるところが好きで使っています。

ひとつ問題になるのがWordPressのslugです。WordPressはpermalinkにslugという投稿のタイトルから作られる文字列が組まれるようになっているのですが、日本語のタイトルを付けているとこのslugの部分がutf8をURLエンコードした文字列になって非常に長くなるので美しくありません。

WordPressのslugを直接設定するためのAPIは存在しないので、けっきょくWordPressにパッチをあてて対処することにしました。一番はじめに保存したときについていたタイトルからslugを設定するようにして、その後はタイトルを変えてもslugは変わらないように書き換えてます。そしてエントリを投稿するときにはまずはじめにてきとうな英語でタイトルを付けてdraftで保存し、そのあとで日本語を含む好きなタイトルを付けてpublishするという運用をしています。

以下WordPress 2.3.3用のそのパッチ。

--- post.org.php        Tue Dec 25 06:24:26 2007
+++ post.mod.php        Tue Feb 12 22:06:42 2008
@@ -726,7 +726,6 @@
                        comment_status = '$comment_status',
                        ping_status = '$ping_status',
                        post_password = '$post_password',
-                       post_name = '$post_name',
                        to_ping = '$to_ping',
                        pinged = '$pinged',
                        post_modified = '".current_time('mysql')."',
@@ -741,9 +740,7 @@
                        VALUES
                        ('$post_author', '$post_date', '$post_date_gmt', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', '$post_status', '$post_type', '$comment_status', '$ping_status', '$post_password', '$post_name', '$to_ping', '$pinged', '$post_date', '$post_date_gmt', '$post_parent', '$menu_order', '$post_mime_type')");
                        $post_ID = (int) $wpdb->insert_id;
-       }

-       if ( empty($post_name) && 'draft' != $post_status ) {
                $post_name = sanitize_title($post_title, $post_ID);
                $wpdb->query( "UPDATE $wpdb->posts SET post_name = '$post_name' WHERE ID = '$post_ID'" );
        }

About this entry