Calling apply_filters on non-documented hooks

Hook use is very, very simple. To create a hook use apply_filters or do_action.

The first parameter is the hook name, the other parameters are the arguments.

You must have the hook name, but the other arguments are optional. You can have, so far as I know, as many arguments as you like. For example:

$t = apply_filters('test_filter',$a,$b,$c,$d);

That act of “applying” the filter is what “triggers” it. This line is where the filter runs. And you can run a set of filters multiple times, not just once.

To add a filter, you create a callback (closures are fine) and add it to the hook. For example:

function test_callback($a,$b,$c,$c) {
  // do stuff
  return $a;
}
add_filter('test_filter','test_callback',10,4);

The fourth argument in the count of arguments you need. Without it, you only get the first argument passed through to your callback.

Actions do not return data, though you can echo it.

Filters should return data, though don’t have to, but know what your are doing if you don’t. Specifically, you should return the data from the first parameter and in the same format– that is, if you get a string, return a string. You should not echo data from a filter as that usually causes very strange effects.

Leave a Comment