Build Your Own Business Website header image

Whoops, you've found some premium content!

Watch the opening clip of this video to preview it,
the full video is available to paid members.

Lesson 8 – Part 2 – Create a Custom WordPress Hook

Difficulty Level -

Filed Under Topics -

Listed Under Lesson Subjects -

Applies to -

What is a Hook?

The very first part of this process is to create a custom WordPress hook. Just to refresh your memory, a hook is a placeholder for a function. Obviously, we’ve talked about this several times in this course when we hooked in the various Thesis hooks or sometimes into WordPress hooks and when the processing of of the page gets to that certain point where the hook is located. If you’ve got a function hook to that hook, then that function will be executed so it’s that placeholder.

Create a Place Holder and a Hook

What we’re going to do today is create the placeholder. What we’ve done before is create functions that hook into existing placeholders but today, we’ll create our own placeholder and we’ll be creating our own hook.

Now, this is one of those places where the documentation is remarkably poor. We’ll start off by taking a look at the WordPress documentation. The function that creates this hook is called the do_action so if you search for it, you can see the WordPress codex pops up here right away. I don’t know who wrote the description for this but as far as I’m concerned it is essentially unintelligible and it does not enlighten the reader.

However, I do have what I consider to be a very good reference for this and that’s the WPCandy Article called, “Introduction to Custom Hooks and Pluggable Functions”. In the course resources, there will be a link to this article. This is an excellent resource for you to read to get a handle on how these hooks are created and how to use them. They’re actually quite easy to create and to use in spite of the fact that they’re not well-documented.

Add Hooks to the Front Page HTML

We’re going to start off with that by creating our own hook here. This is our functions file that we’ve created so far and what I’m going to do is come over to our Library, we’ll open up our functions and we’re going to add a hook to our front_page_html. Let’s go that site for a moment,

What we’re going to do is create a hook that will hook at the end of each one of these widget areas so you can add something to the sidebar after the sidebar. Not that you’re going to use this but it’ll serve as a good example. If we come over to our front_page_html, we’ve got our home_feature_box sidebar which we’re not going to do and we come to our home_sidebars here.

Add Hooks to Widget Areas

These are our 3 widget areas. We’ll go back to PHP and we’re going to go out of PHP so we’ve got that going. Always say do_action and the name of this hook. What I’m going to call it is, hook_after_home_left_widget-area and a semi colon. That’s really all that I have to do to create this hook and I have added this placeholder to this section of the page. In fact, I could create 3 hooks which I think is what I’ll do and hook_after_home_center_widget_area then hook_after_home_right_widget area.

Create Functions that Hook to Widget Areas

Now, I have 3 hooks that I’ve created that are inside of these widget areas. Let’s save that document and open up FileZilla, go to our files and our library, functions and we upload this new front_page_html. Our user could then inside of his custom_functions.php file, create in custom functions something that hooks into that.

Let’s go ahead and open up our custom_functions.php file and pretend we’re a real user. We’ll create a function, function left_message and we’ll just say echo ‘<h3>This is the left message</h3>’ and we would add_action just like always.

Now this is going to be, add_action (‘hook_after_home_left_widget_area’) so there’s the name of our function then the name of the function that we’re hooking to it which is left_message. Let’s do this 3 times, this would be center_message and this would be the right_message. Well, actually this also needs to be the center_hook, left_message.

Our user has now created himself 3 functions that he gets to add to those 3 widget areas. It could be anything, I did this just for ease but if that user uploads the custom_functions.php file now and refreshes this. Isn’t that interesting?

What did I do wrong here? I did the left_message in all places so it’s center_message and right_message. This is the left_message, center_message and right_message. It’s that easy to create the hook which is a placeholder and place it anywhere inside of your theme which will then allow your user to hook into that.

Now obviously, you don’t need the hook, at least not for something like this because you have access to the core files. The purpose for the hook is so that your end user can add some custom functionality of their own without hacking into the core files, that’s what this is all about. This gives your end user a chance to do that.

If you imagine that your end user is going to potentially make some changes or edits to their child theme, adding hooks can be a very useful thing as you’re creating a child theme. You can give them hooks and provide information about those in your documentation.

Save $200 on Membership Now!

Start learning today for as little as
$0.82 PER DAY!
Subscription Options
0 Comments… add one
0 comments… add one

Leave a Comment