Filters and Hooks

Filter Kaapi

Filter Kaapi by Surajram Kumaravel, on Flickr

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.

Facebooktwitterlinkedinyoutubeinstagram

3 thoughts on “Filters and Hooks

  1. Eric Schmitz says:

    D’oh! I didn’t even notice that. LOL! Yes, I do know where my towel is! πŸ˜€ And what an auspicious day to make such a typo, day after Douglas Adams’ birthday. πŸ˜‰

    I think what must have happened is that I “corrected” all the links to the style sheets and javascript files, so the layout became active.

    Won’t hurt to run through that chapter again anyway.

  2. Eric Schmitz says:

    Wow, *neither* of you were kidding about this chapter being a step beyond! I think I will probably go through it a second time, not just to make sure I’ve got it, but also to use the “First Theme” we wrote in the previous lessons. I used the static J2 files, modified as directed, but there’s a lot more code in those files than he covers in this chapter, and my site is starting to actually look something like the completed J2 theme. I get the idea that it shouldn’t look much different than the bare text we already had from before.

    Oh, and yes, I still see WP and think “Word Prefect” — probably because I still sometimes use it! πŸ˜‰

Leave a Reply

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

CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.