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:

Screen Shot 2014-08-15 at 3.16.04 PM

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 = apply_filters( ‘ck_metabox_tabs’, array() );
foreach ( $tabs as $key => $values ) {
?>

  • Posted by Chris Klosowski

    Chris Klosowski is the Director of Technology at Sandhills Development, LLC, creators of fine WordPress plugins, and ecommerce solutions.

    2 Comments

    1. Thank you for the article Chris!

      Reply

    2. My brother suggested I may like this web site.

      He was once entirely right. This post actually made my day.

      You cann’t imagine just how a lot time I had spent for this information! Thank you!

      Reply

    Leave a reply

    Your email address will not be published. Required fields are marked *