WordPress.vim has been missing a very important feature so far, WordPress hooks integration. That’s like eating a Chicken Biryani minus the Chicken! It’s delicious but not quite the real thing.
That’s no more the case! The 0.1.7 release adds some very cool new features to support WordPress Hooks.
The demo below shows the new ex commands like
And if you have CtrlP installed you are going to enjoy,
Initially this was the first thing I wanted to tackle in this plugin. But I was not quite sure how to go about with the implementation.
My initial plans were to use phpdocumentor to generate a list of
filters. Given the
dynamo code already in place this didn’t seem too
But then you don’t just want to go the
invocation of the actions or
filter. It’s far more useful to go to the callback
listeners of these
hooks. Especially for filters where the input may be altered before it
hits your function.
And then there was the whole problem of actually doing something with these generated list of hooks. At the least they needed to be converted to Vimscript and linked in some way to their origin lines.
Finally code generation is fine for syntax highlighting, but for jumping
to source code would be asking for trouble. The lists would need to be
rebuilt every time and would also require
All in all it seemed too tricky, so I had put it on the back burner until today.
An Unexpected Breakthrough
While implementing the WpSeek integration for the
command I had refactored out some code to make jumps to definitions
:Wdef command takes a definition that corresponds to a tag jump.
:Wsimilar command works by finding a list of similar functions and
:Wdef the corresponding user selected match.
In the midst of this I discovered this excellent article by Eric Ritz on teaching exhuberant ctags to understand a new language.
We don’t need to teach
ctags a brand new language, just some new
This was the missing piece I was looking for. It provides an elegant solution to our problem. Just add custom regex patterns to match WordPress actions and hooks.
By adding a custom regex pattern for
add_action and friends we
get new ctags
kinds that correspond to
But how to get the list of all filters and actions so that tab completion would also work?
We can get a list of all the
template_redirect actions with
But taglist needs an
expression for the matched tag name.
The easiest solution is to add a prefix to the
kinds field. For eg:- for
actions if we prefix the
taglist('FOO') gives us a list of tags matching the
FOO pattern, ie:- all the
To avoid accidentally matching
kinds with similar named methods, a UID
action keeps it somewhat readable without
mismatching with WordPress functions. At least until WordPress starts
accepting patches in leetspeak!!!
To round it up the
kinds implementation adds custom regex patterns for
do_action), and their callback listeners(
add_action). And the same for filters and
Note: When upgrading from earlier version to
0.1.7 you need to
regenerate the ctags for these custom patterns. You can use
do this automatically or delete the old tags file and the plugin will
generate a new tags file on the first run.