WordPress教程

WordPress 显示 filter 和 action 钩子上的所有函数

阿里云

WordPress 主题开发或者插件开发时,有时需要列出页面 filter 和 action 钩子上的所有函数,以便调试。主题开发中,将以下代码添加到 functions.php 末尾,即可在访问页面的时候列出 WordPress 钩子上的所有函数:

  1. global $wp_filter;
  2. echo "<pre>" . print_r($wp_filter, true) . "
也想出现在这里?联系我们
创客主机

";

上面的代码会显示附加到 WordPress 所有 filter 和 action 钩子上的函数,而如果想要显示某个 hook 钩子上的函数,则将以下代码添加到 functions.php 末尾:

  1. function print_filters_for( $hook = '' ) {
  2. global $wp_filter;
  3. if( empty( $hook ) || !isset( $wp_filter[$hook] ) )
  4. return;
  5.  
  6. print '<pre>';
  7. print_r( $wp_filter[$hook] );
  8. print '

';
}

通过指定 hook 的名字即可在需要的地方进行调用显示:

  1. print_filters_for( 'the_content' );

对于在页面显示出来的所有函数,如果想要确切知道附加到的 hook 到底是 action 还是 filter 可以通过 这个工具 进行查询。

WordPress 显示 filter 和 action 钩子上的所有函数

已有 156 人购买
查看演示升级 VIP立刻购买

收藏
(1)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!