简体   繁体   English

使用Vim或类似工具查找Wordpress钩子和过滤器

[英]Find Wordpress hooks and filters using Vim or similar

Wordpress allows you to extend its core using "hooks" and "filters." Wordpress允许您使用“挂钩”和“过滤器”扩展其核心。 For example, to execute something quite early in the execution process I may write 例如,为了在执行过程中尽早执行某些操作,我可以编写

add_action( 'init', function() { // Do something } );

Filters work similarly. 过滤器的工作原理类似。 The function that defines a hook is do_action , so to create the init hook a core developer wrote 定义钩子的函数是do_action ,因此要创建核心开发人员编写的init钩子

do_action( 'init' );

do_action accepts several optional arguments that this hook doesn't demonstrate. do_action接受此挂钩未演示的几个可选参数。 The function names for filters are add_filter and apply_filter. 过滤器的函数名称是add_filter和apply_filter。

Say I'm browsing the source code of some plugin and it's using a hook from one of the source files and I don't know which one. 假设我正在浏览某个插件的源代码,并且它使用了源文件之一中的钩子,而我不知道是哪个。 What is the easiest way to locate it? 找到它的最简单方法是什么?

In Vim I use ctags a lot and I was hoping that it was possible to do something similar, only instead of giving the function name I could give the filter or hook name. 在Vim中,我经常使用ctags,我希望可以做类似的事情,只是不能给函数名,而可以给过滤器或钩子名。 Any ideas? 有任何想法吗?

(If it can't be done from within Vim, like ctags, second best would be to run a command that will locate the script for me. This could also be acceptable if it is the best solution) (如果不能像ctags这样在Vim内完成操作,那么最好的办法就是运行一个命令来为我定位脚本。如果这是最好的解决方案,那也可以接受)

Say I'm browsing the source code of some plugin and it's using a hook from one of the source files and I don't know which one. 假设我正在浏览某个插件的源代码,并且它使用了源文件之一中的钩子,而我不知道是哪个。 What is the easiest way to locate it? 找到它的最简单方法是什么?

I use grep. 我用grep。 Couldn't work without it. 没有它就无法工作。 grep gets me just about anything I want. grep让我几乎得到任何想要的东西。 grep -Rni "do_action( 'init'" * should find the 'init' hook. You can use regex in the search string if you need to as well as tell it to ignore particular files and/or directories. I've tried other options but just haven't ever been sold on anything else. grep is quick and clean. grep -Rni "do_action( 'init'" *应该找到'init'钩子。如果需要并告诉它忽略特定文件和/或目录,则可以在搜索字符串中使用regex。选项,但只是没去过上销售的其他任何东西。 grep是快速,干净。

This isn't a 'Vim' answer but you did say 'or similar' :) 这不是“ Vim”答案,但您确实说了“或类似” :)

There is a very good hook database at http://adambrown.info/p/wp_hooks/version/3.4 too. http://adambrown.info/p/wp_hooks/version/3.4上也有一个非常好的钩子数据库。

I've had to do this quite a lot lately so I wrote a little Vim plugin to ease the process. 我最近不得不做很多事情,所以我写了一个Vim插件来简化这个过程。

Simply place the caret on name of the hook or filter, press Leader + f to find the hooked functions, or Leader + F to find the hook/filter definition. 只需将插入号放在挂钩或过滤器的名称上,按Leader + f查找挂钩函数,或按Leader + F查找挂钩/过滤器定义。

To install: 安装:

cd ~/.vim/bundle && git clone https://github.com/borzhemsky/wp-hook-finder.git

Add the keybindings to ~/.vimrc: 将键绑定添加到〜/ .vimrc中:

nnoremap <Leader>f :FindWPHook<CR>
nnoremap <Leader>F :FindWPHookDef<CR>

Site note: Writing this taught me that, yes, people do things like using add_filter hook their functions to hooks defined with do_action , so searching for occurrences manually can be error-prone. 站点注释:写这篇文章告诉我,是的,人们做诸如使用add_filter其函数挂钩到使用add_filter定义的挂钩之类的do_action ,因此手动搜索出现的内容很容易出错。

From the basic to the rather involved: 从基本到相当复杂:

GREP 格雷普

Already explained. 已经解释了。

GREP FROM VIM OR VIMGREP 来自VIM或VIMGREP的GREP

If you are already in Vim, you can use the :vimgrep command or its sister :grep : 如果您已经在Vim中,则可以使用:vimgrep命令或它的姐妹:grep

:vim "do_action( 'init' )" **/*.php | copen

See :help starstar for the ** wildcard that let's you do recursive search. 请参阅:help starstar以获取使您可以进行递归搜索的**通配符。 :vimgrep uses an internal method while :grep uses, well… grep . :vimgrep使用内部方法,而:grep使用,嗯… grep The latter may be faster. 后者可能更快。

CTAGS 标签

Assuming you have ctags installed, the indexing can be done in the shell or in Vim: 假设您已安装ctags ,则可以在shell或Vim中进行索引:

$ ctags -R . <-- in the shell
:!ctags -R . <-- in Vim

and the querying is just a matter of :tag do_action . 和查询只是问题:tag do_action Read :help tags for an indepth explanation. 阅读:help tags以获得更深入的解释。

CSCOPE 范围

Assuming you have cscope installed, you can use it right from the shell: 假设您已安装cscope ,则可以从外壳中直接使用它:

$ cscope -R *.php

Once the index is created you can search in cscope 's interface and open the chosen files in your editor. 创建索引后,您可以在cscope的界面中搜索并在编辑器中打开所选文件。

Assuming your Vim comes with cscope support, you have to: 假设您的Vim带有cscope支持,则必须:

  1. create the index, :!cscope -bR *.php 创建索引, :!cscope -bR *.php

  2. locate the index, :cs add cscope.out 找到索引, :cs add cscope.out

  3. *f*ind the *d*efinition, :cs fd do_action * f *找到* d *定义, :cs fd do_action

There are other specialized tools like Codesearch or GNU Global but I think that you don't really need to go further than plain grep or, at most, ctags . 还有其他专门工具,例如CodesearchGNU Global,但我认为您真的不需要比普通grep或最多ctags更进一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM