The Genesis Framework is a remarkable development tool for building high quality WordPress sites quickly and competently. Understanding how the framework can help accomplish this goal can be somewhat elusive though. Detailed technical documentation is often lacking and walking through code to discover the hows and whys of things is sometimes the only way to learn.
I spend about an hour a day volunteering to answer questions on both the Studio Press Community Forum and on the Genesis WordPress Facebook page. I love helping people and teaching them to do things better. I don't always have the best answers, but over the last few years, I've literally provided thousands of responses to questions at all technical levels, questions that range from "How do I do such and such in CSS?" to "How do these filters work?"
Most recently, a poster on the Studio Press Community Forum asked a question that had me a little stumped and I gave an answer that worked to solve his problem, but it turned out not to be the best answer. In fact, after doing some research the original poster provided a better answer than the one I had given. This got me to thinking. Maybe I was being a little too lazy that day, or perhaps I was time constrained because of other commitments, but I could have walked through the Genesis code more deeply than I had and gotten to the better answer.
The better answer was to use one of the Genesis Dynamic Filters, but wouldn't it be nice if there were better documentation about Genesis Dynamic Filters? It would save a lot of code walking and bring us to problem solving solutions much faster. So this is the first of a series of articles to teach more about Genesis Dynamic Filters, what they are, and how to use them. If your preference is for visual learning, rather than reading, then I highly recommend Tonya Mork's Know the Code website. If you prefer reading, then please keep going. This series of articles focus on filters that can be used to customize Genesis.
WordPress Plugin API
When it comes to professional WordPress development, it is absolutely imperative we understand that the WordPress developers have given us a way to change system behaviors through events where we can register our own functionality. They've done this so we, as developers, can enhance the usability of WordPress and create a more feature rich environment. We can go in and tweak content, manipulate data, and do all kinds of fun things if we know how to use the event registry features of the WordPress plugin API. These events are known as hooks. Hooks are comprised of actions and filters.
Actions and filters are defined and executed in very similar ways, the primary difference is that a filter is a function that returns a value. Filters are primarily responsible for intercepting, managing, and returning content before rendering it to the browser, or saving data from the browser to the database. Actions are functions that are executed at specific points in the page rendering sequence. They do not return a value.
The two primary functions that we will look at in reference to Genesis is add_filter() and apply_filters().
- add_filter() registers your callback (function) to the specified event (filter hook). When the event fires, the callback is executed in the order of the specified priority. The default priority in WordPress is 10.
- apply_filters() fires the specified event, which means it loops through the event registry lookup table (array) and calls each registered callback one-by-one in the order of their priority. The callbacks are registered for this event using add_filter.
These functions let you make changes to themes or templates without editing the original file. However, the template or theme needs to have apply_filters() wrapped in an editable area before you can use add_filter() to make the edits via your own callback (function).
We'll dive more deeply into this concept as we continue through this tutorial. For now and for your convenience, here is a list of all WordPress filters, and there are a lot of them.
What are Genesis Dynamic Filters?
The Studio Press developers are very smart. They generally write very efficient, easy to understand code. While there are a few things I've seen in the Genesis code base that I may have done differently to provide greater efficiency or better readability, as a general statement, the code base is written for efficiency. One way the developers have done this is by applying filters dynamically. The Genesis code base includes several functions where apply_filters() is invoked based on specific contexts rather than being hard coded into the framework. You'll still find some hard coded apply_filters() in the framework, but the majority are applied dynamically.
We're going to examine how these dynamic apply_filters() are created in Genesis by looking at the code used to create structural wraps. But before we move into the code walk through, let me mention one PHP debugging tool that I find to be invaluable. That tool is Kint. It works very well with WordPress. I install it on every site I build while it is in the construction phase. It is an excellent replacement for var_dump(), print_r(), and debug_backtrace(). It is simply called by adding d() or ddd() in your code. We'll be using Kint as we walk through the Genesis code.