I’ve previously written a few posts about using WordPress Actions and Filters to better extend your plugins and/or themes. This time though, I want to talk about leading by example, and using your own hooks and filters to add functionality. What does this mean? It means where a hook or filter exists, you should use it to add your built in functionality. It’s probably easiest to explain by example…so here is a recent issue I ran into. I wanted the ability to create an arbitrary number of ‘tab’ sections in a plugin settings page I was building. Something like this:

Now, hard coding a list of tabs is easy, but what if I didn’t want to always show the tabs, just the ones that were active? Well, I could simply make the action of outputting the tabs, an filter that get’s looped on. Like this:
[php light=true]
function ck_generate_tabs() {
tabs as values ) {
?>
$i++;
}
}
[/php]
If we go no further, we’ll have an empty set, and nothing will foreach on, however, if we do something like the following, we’ll add an element to the array, therefore allowing a loop and generating tabs:
[php light=true]
function ck_tw_add_meta_tab( tabs ) {
tabs[‘tw’] = array( ‘name’ => __( ‘Twitter’, ‘ppp-txt’ ) );
return $tabs;
}
add_filter( ‘ck_metabox_tabs’, ‘ck_tw_add_meta_tab’, 10, 1 );
[/php]
This is great for 2 reasons.
- It makes sure our hooks and filters work!
I can’t tell you how important this is. By using it, we’ve essentially tested it works as expected, which will make other developers happy. - It provides other developers a ‘guide’ on how to extend your platform.
The chain we end up with is, you call ck_generate_tabs which will then run the apply_filters on ck_metabox_tabs, which then triggers ck_tw_add_meta_tab to add an item to the empty array and then return it, allowing the original function to generate the tabs.
This example is clearly missing styles and JavaScript to allow this to fully function, but this should get you an example of how hooks and filters in your own code, that you, yourself are using.