Now that you understand Thesis 2.1 custom PHP files, next we’re going to talk about WordPress action hooks and filter hooks and how we’ll be using them in Thesis 2.1.
First, I’d like to direct your attention for a moment to the main seminar page here, Introduction to Customizing Thesis 2.1 Using PHP. Because this is where you’ll find links to all the videos and seminar resources. In particular I have links to some of the primary documentation elements and then a cool tool I created for you, the sample box that’s going to get you started with creating your own box.
How Action Hooks Work
We’re going to start off by looking at action hooks. Now, I’m often surprised at how convoluted people’s descriptions of action hooks are. Essentially, an action hook is a placeholder. It’s a WordPress function so it’s not a PHP function. It’s a WordPress function that is a place where you can attach your own functions.
So what happens is, WordPress is working along going through its normal cycle and gets to a hook location in that cycle and then it asks, “Is anything hooked to this? So it’s this location where WordPress looks for other functions in the site that somebody can choose to hook into. And that’s an action hook.
2 Things an Action Hook Can Do
Action hooks can do two things, they can add functionality to the hook and they can remove functionality from the hook. While we don’t do any of this or I haven’t found an occasion to do it yet in Thesis 2, I used to do this all the time in Thesis 1.8.5.
An example would be to remove Action Thesis Hook Header and Thesis Nav Menu, right? So you’d take the nav menu out of the top of the header and then you’d add it back to the bottom of the header. Essentially you’d remove it from hook and add it back to another hook.
There were lots of instances of this remove action, add action behavior in Thesis 1.8 whereas now, I think there’s probably almost no need for that anymore in Thesis 2.1 but in any case, it’s common, there are both add actions and remove actions in WordPress at those action hooks.
Action Hook Locations
Action hooks are actually located in a couple of different places or kind of conceptually different places. The first place they’re contained in is inside the processing of a web page. So, this is before any output is generated but when it is being processed. And the second is within the output of the web page. So after the webpage is being generated at some specific location inside the page, action hooks can fire and things can happen.
Some Common Action Hooks and Their Locations
Most WordPress action hooks exist inside the processing part of the webpage and so very common WordPress action hooks are like “init”.
The Init Hook is a hook that fires at the initialization of WordPress. So way before WordPress figures out which theme is being used, way before it tries to calculate what page should be displayed or anything like that, you have this Init Hook.
Another very common WordPress hook is Plugins Loaded. Now, the Plugins Loaded Hook fires as soon as all of the plugins have been loaded into memory and before most of their actions take place so this happens after Init but before lots of other stuff.
Theme Set Up
Another common one is Theme Set Up. A Theme Set Up Hook fires at the beginning of the process of setting up the theme and each of these hooks fires on every single page view.
So every time you move from one page to another, you have this entire process where first, your server says, “Oh yeah, it’s WordPress”, and then second WordPress says, “Okay, what things do we have to initialize?”, “What plugins are we going to load?”, “What theme are we going to use?”, “What theme settings are we going to apply?.
And then it moves into what is the query variable or that is, what page wants to be displayed. And then when that happens, what template inside this gets displayed.
So there’s this process that WordPress goes through every time it processes a page and that process is punctuated by action hooks.
WP Head and WP Footer
Inside the processing of a page, WordPress has a couple of other action hooks, actually it has several but there’s only two of them that get used a lot. Those are WP_Head and WP Footer and we’re going to see what those look like here in a minute.
WP_Head is essentially a hook that’s inside of the head tag in the HTML page and WP Footer fires immediately before the closing body tag of the HTML and so those are links inside the page.
Action Hooks WordPress vs Thesis 1.8.5
Now, if you come to this from Thesis 1.8.5, you’ll probably recognize that almost all of the hooks that Thesis 1.8.5 had were actually inside the processing of a page. So we had a diagram where you could look at and say, here’s the Thesis Hook Header, here’s the Thesis Hook Before Header, here’s the Thesis Hook After Header and all of these hooks are actually happening inside the page.
It actually did have a couple of hooks inside the processing of the page but nobody ever used them and we don’t really know how they work because they were used internally for Thesis and was something that programmers generally attempted to use.
So that’s the contrast really between Thesis 1.8.5 and WordPress. WordPress contains most of its hooks during the processing and Thesis contained most of its hooks during the output of the page, okay that’s an action hook.
What is a Filter Hook?
A filter hook is also a WordPress function and it’s similar to an action hook except that it’s not a stopping place where WordPress says, “Okay, anybody else want me to do something? Going once, going twice, going three times, okay we’ll keep going then”, that’s the action hook behavior.
The filter hook behavior is when WordPress has some block of data that it’s getting ready to output or use and then it gives you the opportunity to filter that data. And by filter what it means is, add, subtract, substitute information. Essentially, a filter hook contains some value that you can manipulate.
How a Filter Hook Works
The most common example of this is the WordPress filter hook, the “Content”. You know most plugins say, “Put comments at the bottom of a post”, or “Put social sharing buttons at the bottom or at the top of the post”. And they do that using the the Content Filter.
And how this works is that WordPress generates its content, places it in this filter and then the plugin calls the Content Filter. It takes the content and then adds some content to the end of it and then returns the result and that is a filter hook.
A filter hook is essentially a block of data that WordPress is getting ready to use in one way or another and it gives you the opportunity to intercept that block of data and change it in some way before WordPress continues to process it.
So where we made the distinction with action hooks that some are in the processing of the page and some are in the content of the page, that distinction doesn’t really matter with filter hooks.
The reason the distinction matters in action hooks is because one thing happens before another and then another thing happens before another. So you’ve got this sequential list of things that happen starting at the beginning and ending at the end and action hooks fit in specific locations in that process.
Filter hooks aren’t like that. Filter hooks will always fire on the filter that’s defined and it doesn’t really matter where that filter is and it also doesn’t matter where you call the filter. The filter is going to fire whenever the filter is generated or whenever the filter is created inside of WordPress.
Anyway, common examples of filter hooks are, of course, the content or the excerpt which is what we will be using today, we’re going to be using the Excerpt Filter. There’s also a log in header title so if you want to change your WP Admin, when you go and get your login page you can.
Let’s just go here I’ll show you what I mean. If I type in wp-admin, this little thing right here, “Powered by WordPress”, this is the title that WordPress creates and it has a filter called Login Header Title which you can use to change this.
So instead of “Powered by WordPress”, it could say, “Powered by Fishsticks”, it doesn’t really matter what it is, you can replace this with a filter by returning different content than is inside the filter.
Two Filters we’ll Use
The two that we will be working with today are excerpt_length and excerpt_more.
excerpt_length is essentially a counter that is automatically set to 55 that WordPress uses for deciding how many words should show in the excerpt and so we will be using the Excerpt Length Filter to change that counter from 55 to whatever we want it to be.
excerpt_more is a filter that WordPress uses for creating things. Well, it’s used specifically for manipulating. We need to change this actually because right now we’re showing the content and if we show the content it’s not going to be very helpful to us.
Review Existing Excerpt
So we’re going to open up the tray and drag the Content and drop it out and then open up the Excerpt and bring the Excerpt up. Let’s view this thing, so now here’s our Excerpt. This little thing right there, that is the default content of the excerpt_more filter. And what WordPress does is it shows that by default but the excerpt_more filter allows you to change that.
Use of Action and Filter Hooks in 2.1 vs 1.8.5
Now, something to say about all of this is that in Thesis 1.8.5, very few people ever worked with filters and most people thought filters were confusing. But people worked with action hooks all the time. Well, with Thesis 2.1 the chances are, you’re not going to work with action hooks nearly as often because you don’t really need to but you are going to work with filter hooks a lot more because Thesis has a whole ton of filter hooks that you will undoubtedly use.
Also all of WordPress’ filter hooks are available to you to manipulate and so you’re going to find that you’ll be working in filter hooks probably more than you’re working with action hooks and this is one of the differences in 2.1 and 1.8.5. Okay, so we’re going to write our very first function using a WordPress action hook.
Actually, I did want to draw your attention to this, this is the Plugin API/Action Reference that you can get to from this link here and here is the Filter Hooks Reference. Both of these things have long lists of either the filter hooks or the action hooks available to you. This isn’t an exhaustive list of the hooks but it’s lots and lots of them. You can see there are hundreds of action hooks and action filters available for your use in your programming.