WordPress allows you to extend its core using “hooks” and “filters.” 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 accepts several optional arguments that this hook doesn’t demonstrate. The function names for filters are add_filter and 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. 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)
I use grep. Couldn’t work without it.
grep
gets me just about anything I want.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.This isn’t a ‘Vim’ answer but you did say ‘or similar’ 🙂
There is a very good hook database at http://adambrown.info/p/wp_hooks/version/3.4 too.
I’ve had to do this quite a lot lately so I wrote a little Vim plugin to ease the process.
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.
To install:
Add the keybindings to ~/.vimrc:
Site note: Writing this taught me that, yes, people do things like using
add_filter
hook their functions to hooks defined withdo_action
, so searching for occurrences manually can be error-prone.From the basic to the rather involved:
GREP
Already explained.
GREP FROM VIM OR VIMGREP
If you are already in Vim, you can use the
:vimgrep
command or its sister:grep
:See
:help starstar
for the**
wildcard that let’s you do recursive search.:vimgrep
uses an internal method while:grep
uses, well…grep
. The latter may be faster.CTAGS
Assuming you have
ctags
installed, the indexing can be done in the shell or in Vim:and the querying is just a matter of
:tag do_action
. Read:help tags
for an indepth explanation.CSCOPE
Assuming you have
cscope
installed, you can use it right from the shell:Once the index is created you can search in
cscope
‘s interface and open the chosen files in your editor.Assuming your Vim comes with cscope support, you have to:
create the index,
:!cscope -bR *.php
locate the index,
:cs add cscope.out
*f*ind the *d*efinition,
:cs f d 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
.