Filters and Hooks
I found this part of the book the most difficult to comprehend. Up until now everything was still within the realm of my experience with computers (starting back in the early eighties), HTML and CSS. Figuring out PHP was not that hard, either. But Jesse wasn’t kidding when he warned us this section would be harder.
I think I get the concept of a filter. Our PHP functions are taking information from the WP settings and putting it into the HTML files that create the actual website. That dynamic website building is very, very cool if you ask me. The filter allows plugins to intercept that information and change it before it makes it into the HTML. I find that both scary and cool.
I had to look up the concept of an action hook in WordPress. As I understand it, hooks are the way of connecting things that we write to the core WP functions that run everything else. (Just as an aside, any other old geezers first read “WordPerfect” when they see the initials WP??).
On pages 78 through 80, Jesse walks us through a PHP function he has written that filters the site title and modifies it depending on where in the site the user is, and displays the modified title accordingly. I’m not going to repeat it here – if you’ve ever done any programming with simple Boolean logic, it’s not a complicated program. The only intimidating thing about it is that it’s in PHP. If you haven’t, I’ll be happy to explain it to you privately. Just drop me a line.
As mentioned, I was thrown by the concept of “hooking” into ‘wp_title’, but I think I understand it now. wp_title is a WordPress function that already exists, and the hook allows us to connect our filter function to it, so that any time anybody using our theme makes a title, wp_title will use our filter instead of whatever standard filter came with it.
He does similar things to bring in meta information (important for SEO) and to connect to the stylesheet. He briefly discusses the action hook ‘wp_head’ but doesn’t really explain it (I’m sure there will be more to come), a few things that need to go into the body, the header and footer functions.
So far nothing much has happened, but we’ve laid the foundation for building menus into our theme, which really blew my mind and will probably bemuse yours, as well.
Feel free to ask questions – I will answer them to the best of my ability.