Google Crisis Response(Google)
災害に関する情報源や、行方不明者情報の収集と検索を行う『パーソンファインダー』を初めとするツールの提供が行われています。

2007年2月15日 木曜日

WordPress プラグインの仕組み

Filed under: EntryKeywords,WordPress,ハック,プラグイン
時間:3時25分
投稿者:よしとも
AddClips 経由でソーシャルブックマークに登録

 自作プラグイン EntryKeywords のオプション設定画面を CSS で装飾したくて作業中。

 マニュアルを見ていて add_action('admin_head ', '関数'); とすればいいはずと目星をつけていましたが、なぜか動いてくれません。2日以上悩み続けています。仕方がないので流れを追いかけてみることに。
 結局動くまでには至っていませんが、せっかくなので仕組みをメモ。

登録

 まず、関数 add_action でフックに対応する関数の登録を行います。2.0.7 では add_filter を呼び出すだけなので、実際の処理は 関数 add_filter を見ます。定義は、/wp-includes/functions.php にあります。

  1. function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
  2.     global $wp_filter;
  3.  
  4.     // check that we don't already have the same filter at the same priority
  5.     if ( isset($wp_filter[$tag]["$priority"]) ) {
  6.         foreach($wp_filter[$tag]["$priority"] as $filter) {
  7.             // uncomment if we want to match function AND accepted_args
  8.             // if ( $filter == array($function, $accepted_args) ) {
  9.             if ( $filter['function'] == $function_to_add ) {
  10.                 return true;
  11.             }
  12.         }
  13.     }
  14.  
  15.     // So the format is wp_filter['tag']['array of priorities']['array of ['array (functions, accepted_args)]']
  16.     $wp_filter[$tag]["$priority"][] = array('function'=>$function_to_add, 'accepted_args'=>$accepted_args);
  17.     return true;
  18. }

 フックの登録は、グローバル変数 $wp_filter に配列として登録されます。関数 add_filter の処理は大きく2つに分かれていて、登録処理は後半の部分で行います。前半は重複して登録しないためのチェックです。
 登録処理のコメントにもありますが、登録情報はフックと優先度で分類されています。また、関数にはオブジェクトのメンバー関数も登録できます。その場合は、array(オブジェクトインスタンス, 関数名) という配列を使います。オブジェクトインスタンスは参照渡しする必要があります。そのため、PHP 4 では変数の頭に & をつけて明示的に参照渡しにする必要があります。

実行

 登録した関数を実行するには、関数 do_action を使います。この定義も /wp-includes/functions.php にあります。

  1. function do_action($tag, $arg = '') {
  2.     global $wp_filter;
  3.     $extra_args = array_slice(func_get_args(), 2);
  4.     if ( is_array($arg) )
  5.         $args = array_merge($arg, $extra_args);
  6.     else
  7.         $args = array_merge(array($arg), $extra_args);
  8.  
  9.     merge_filters($tag);
  10.  
  11.     if ( !isset($wp_filter[$tag]) ) {
  12.         return;
  13.     }
  14.     foreach ($wp_filter[$tag] as $priority => $functions) {
  15.         if ( !is_null($functions) ) {
  16.             foreach($functions as $function) {
  17.  
  18.                 $function_name = $function['function'];
  19.                 $accepted_args = $function['accepted_args'];
  20.  
  21.                 if ( $accepted_args == 1 ) {
  22.                     if ( is_array($arg) )
  23.                         $the_args = $arg;
  24.                     else
  25.                         $the_args = array($arg);
  26.                 } elseif ( $accepted_args > 1 ) {
  27.                     $the_args = array_slice($args, 0, $accepted_args);
  28.                 } elseif ( $accepted_args == 0 ) {
  29.                     $the_args = NULL;
  30.                 } else {
  31.                     $the_args = $args;
  32.                 }
  33.  
  34.                 $string = call_user_func_array($function_name, $the_args);
  35.             }
  36.         }
  37.     }
  38. }

 前半は飛ばして、後半の foreach を見ます。
 特定のフックに対する登録情報を順に処理しています。情報を取り出し、引数の処理をし、最後に組み込み関数 call_user_func_array で関数をコールします。

どこまでは動いているのか

 調べた結果、少なくとも add_filter での重複チェックを通過していることはわかりました。最後の return の直前($wp_filter への登録直後)で $wp_filter[$tag]["$priority"] の値を表示させて確認しました。
 add_action を経由しないで直接登録した場合ではきちんと動作しましたので、何らかの理由により登録を削除されているのではないかと考えています。

参考になりましたか?
1 Star2 Stars3 Stars4 Stars5 Stars (未評価)
by WP-PostRatings
Loading ... Loading ...

1件のコメント »

  1. WordPress プラグインのトラブル解決…

    散々悩んだプラグインの問題ですが、原因は単なる入力ミスでした。思わず苦笑いです。

    (more…)

    トラックバック by 雑念ベース — 2007年2月15日 木曜日 @ 4時14分

この投稿へのコメントの RSS フィード。 TrackBack URI

コメントする

HTML convert time: 0.147 sec. Powered by

Images is enhanced with WordPress Lightbox JS by Zeo