2011-03-07 81 views
41

我有一個關於WordPress的問題,特別是版本3.0和更新。WordPress的:我如何獲得所有'the_content'過濾器的註冊功能

有誰知道如何獲得將被應用或'註冊'到the_content過濾器的所有函數的數組或列表?

這個想法是生成一個可選功能的複選框列表,以從過濾器中刪除,如wpautop。我知道如何使用硬編碼標籤去除過濾器中的函數,但我希望能夠創建更加動態的解決方案。

如果任何人有任何想法,如果這是可能的,它是如何做到的,我會非常感興趣。謝謝。

回答

94

從過濾器陣列打印簡單功能?

function print_filters_for($hook = '') { 
    global $wp_filter; 
    if(empty($hook) || !isset($wp_filter[$hook])) 
     return; 

    print '<pre>'; 
    print_r($wp_filter[$hook]); 
    print '</pre>'; 
} 

調用它你需要它的地方。

print_filters_for('the_content'); 
+3

真棒。感謝您的回覆,我不知道wp_filter全局,但現在這一切都有道理。歡呼:) – macguru2000 2011-03-09 04:47:03

+0

作品像一個魅力!謝謝! – 2015-05-20 11:49:39

17

這是更高級的例子的位,這將在除了數據從$wp_filter陣列,表示使鉤附連的文件的路徑,以及在代碼行的函數被定義。

要獲取特定操作(或過濾器)上掛鉤的函數的基本列表,只需從過濾器數組中獲取項即可,但由於函數可以以各種方式附加(作爲類方法或閉包)該列表將包含大量包含呈現爲字符串的對象的相關數據。這個例子將只顯示相關的數據,按照優先級的順序:

  • 函數名(取決於回調語法):
    • 回調函數:'function_name'
    • 對象方法:array($object, 'function_name')
    • 靜態類方法:array('class_name', 'function_name')'class_name::function_name'
    • 閉合:function() {}
    • 相對靜態類方法:array('class_name', 'parent::function_name')
  • 接受ARGS
  • 文件名
  • 開始行
  • ID
  • 優先

function list_hooks($hook = '') { 
    global $wp_filter; 

    if (isset($wp_filter[$hook]->callbacks)) {  
     array_walk($wp_filter[$hook]->callbacks, function($callbacks, $priority) use (&$hooks) {   
      foreach ($callbacks as $id => $callback) 
       $hooks[] = array_merge([ 'id' => $id, 'priority' => $priority ], $callback); 
     });   
    } else { 
     return []; 
    } 

    foreach($hooks as &$item) { 
     // skip if callback does not exist 
     if (!is_callable($item['function'])) continue; 

     // function name as string or static class method eg. 'Foo::Bar' 
     if (is_string($item['function'])) { 
      $ref = strpos($item['function'], '::') ? new ReflectionClass(strstr($item['function'], '::', true)) : new ReflectionFunction($item['function']); 
      $item['file'] = $ref->getFileName(); 
      $item['line'] = get_class($ref) == 'ReflectionFunction' 
       ? $ref->getStartLine() 
       : $ref->getMethod(substr($item['function'], strpos($item['function'], '::') + 2))->getStartLine(); 

     // array(object, method), array(string object, method), array(string object, string 'parent::method') 
     } elseif (is_array($item['function'])) { 

      $ref = new ReflectionClass($item['function'][0]); 

      // $item['function'][0] is a reference to existing object 
      $item['function'] = array(
       is_object($item['function'][0]) ? get_class($item['function'][0]) : $item['function'][0], 
       $item['function'][1] 
      ); 
      $item['file'] = $ref->getFileName(); 
      $item['line'] = strpos($item['function'][1], '::') 
       ? $ref->getParentClass()->getMethod(substr($item['function'][1], strpos($item['function'][1], '::') + 2))->getStartLine() 
       : $ref->getMethod($item['function'][1])->getStartLine(); 

     // closures 
     } elseif (is_callable($item['function'])) {  
      $ref = new ReflectionFunction($item['function']);   
      $item['function'] = get_class($item['function']); 
      $item['file'] = $ref->getFileName(); 
      $item['line'] = $ref->getStartLine(); 

     }  
    } 

    return $hooks; 
} 

由於掛鉤可廣告DED以及整個運行時取出,輸出在什麼時候被調用的函數依賴於(wp_footer行動是一個很好的地方,以獲得完整的列表)

print_r例如爲the_content過濾器:

Array 
(
    [0] => Array 
     (
      [id] => 000000004c8a4a660000000011808a14run_shortcode 
      [priority] => 8 
      [function] => Array 
       (
        [0] => WP_Embed 
        [1] => run_shortcode 
       ) 

      [accepted_args] => 1 
      [file] => C:\xampp\htdocs\wordpress\wp-includes\class-wp-embed.php 
      [line] => 58 
     ) 

    [1] => Array 
     (
      [id] => wptexturize 
      [priority] => 10 
      [function] => wptexturize 
      [accepted_args] => 1 
      [file] => C:\xampp\htdocs\wordpress\wp-includes\formatting.php 
      [line] => 41 
     ) 

    [2] => Array 
     (
      [id] => 0000000006c5dc6d0000000064b1bc8e 
      [priority] => 10 
      [function] => Closure 
      [accepted_args] => 1 
      [file] => C:\xampp\htdocs\wordpress\wp-content\plugins\plugin\plugin.php 
      [line] => 16 
     ) 

    ..... 

編輯:2017年5月5日

  • 適合WP_Hook
  • 添加優先
  • 固定:錯誤引發,如果回調不存在,但WordPress的也提出了爲
  • 固定警告:鉤具有相同ID,但不同的優先級會覆蓋前一個
+4

真棒調試「工具」 - 做得很好。 – 2014-12-16 22:16:04

+0

僅供參考 - 我嘗試在我的插件中使用它,但它需要更多的錯誤捕獲,因爲如果有人添加一個不存在的函數/方法,它會生成一個致命錯誤:'致命錯誤:未捕獲的異常'ReflectionException' Walker_Nav_Menu()不存在'' – helgatheviking 2016-02-16 15:24:18

+0

我添加了一個'try/catch'的味道[這裏](https://gist.github.com/helgatheviking/2d9e1208a96978b2154b)。 – helgatheviking 2016-02-16 15:50:45

相關問題