wordpress 本体コード応用術
DESCRIPTION
WordPress のサイトに新しい機能を導入したいとき、独自にコードを書き出すのではなく、最初に WordPress 本体 (Core) 内のコードで再利用できるものを探す、というアイデアを提案します。TRANSCRIPT
WordPress本体コード応用術WordPress 研究会 @ 下北沢 OSS Cafe, 2011-08
みっちょ (アーリーワイン・マイケル芳貴)
mitcho.com @themitcho slideshare.net/mitcho
どうも。みっちょです。
新機能を一から作らないでWordPress 本体内のコードを再利用しよう
抽象化を使用しよう
• DB クエリ: mysql_* じゃなくて $wpdb• キャッシュ: DB じゃなくて Object Cache、
Transients• 環境設定: DB じゃなくて set_option()• 投稿抽出: SQL じゃなくて get_posts()
• HTTP 通信: curl じゃなくて wp_remote_post()
コードをより WordPress っぽく
コードを読めば公開 API 以外も応用できる
実例その1*_meta
投稿、ユーザは meta が着く
問題:taxonomy、コメントにも meta が欲しい...
function add_user_meta($user_id, $meta_key, $meta_value, $unique = false) { return add_metadata('user', $user_id, $meta_key, $meta_value, $unique);}
function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) { // make sure meta is added to the post, not a revision if ( $the_post = wp_is_post_revision($post_id) ) $post_id = $the_post;
return add_metadata('post', $post_id, $meta_key, $meta_value, $unique);}
実は...
裏で同じ関数を使用
function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) { if ( ! $table = _get_meta_table($meta_type) ) return false;... $result = $wpdb->insert( $table, array( $column => $object_id, 'meta_key' => $meta_key, 'meta_value' => $meta_value ) );...}
function _get_meta_table($type) { global $wpdb;
$table_name = $type . 'meta';
if ( empty($wpdb->$table_name) ) return false;
return $wpdb->$table_name;}
$wpdb->... にテーブル名を入れておけば使える($wpdb->{table} が指定される時: init & switch_blog)
Taxonomy Metadatawordpress.org/extend/plugins/taxonomy-metadata
後は UI を足せばいいだけ
実例その2pub/sub
WordPress 3.2 で集中執筆モードが導入された
問題:集中しすぎちゃう...
新問題:集中執筆モードと同時に作動したい
JavaScript では action/filter が(今の所)ない。
(function($){ var api, ps, bounder, s;
// Initialize the fullscreen/api object fullscreen = api = {};
// Create the PubSub (publish/subscribe) interface. ps = api.pubsub = new PubSub();
ps.subscribe( 'showing', function() { // This event occurs while the DFW overlay blocks the UI. });
ps.subscribe( 'hidden', function() { // This event occurs after DFW is removed. });
});
/** * PubSub * * A lightweight publish/subscribe implementation. * Private use only! */var PubSub, fullscreen, wptitlehint;
PubSub = function() { this.topics = {};};
PubSub.prototype.subscribe = function( topic, callback ) {};
PubSub.prototype.publish = function( topic, args ) {};
非公開 API 発見。これぞ action の JavaScript 版!
(function($){ var api, ps, bounder, s;
// Initialize the fullscreen/api object fullscreen = api = {};
// Create the PubSub (publish/subscribe) interface. ps = api.pubsub = new PubSub();
ps.subscribe( 'showing', function() { // This event occurs while the DFW overlay blocks the UI. });
...
var PubSub, fullscreen, wptitlehint;
fullscreen は window のオブジェクトなのでアクセス可fullscreen.pubsub.subscribe で自分の関数を登録...
動いたニャン。
デモwordpress.org/extend/plugins/nyan-cat/
実例その3Query/River
WordPress で使える Quicksilver が欲しい
問題:検索 API を作りたくない
そういえば...
var wpLink;(function($){ var inputs = {}, rivers = {}, ed, River, Query;
wpLink = { }
River = function( element, search ) { }; $.extend( River.prototype, { });
Query = function( search ) { }; $.extend( Query.prototype, { });})(jQuery);
River と Query は window からアクセス出来ない (涙)
デモこのプラグインは現在コアパッチも
必要なので未公開 :(
ご清聴ありがとうございます
みっちょ (アーリーワイン・マイケル芳貴)
mitcho.com @themitcho slideshare.net/mitcho