Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
WordPress functions are coded to have what are referred to as hooks, so that they can be easily customized. There are two basic types of hooks: actions and filters, the latter of which we've used repeatedly in our functions.php file so far. Let's take a closer look at the code to create a filter hook first and then add an action hook. As the name suggests, a filter shifts and frequently modifies the data before it's displayed in the browser. I have opened now my functions.php file as you find within the tpa theme folder, and I am going to scroll down.
Here you see two filter hooks. A filter hook has two parts: the function and the add_filter statement. The filter function is often used to define new settings for a built-in function. Let's take a look at the new_excerpt_ length function that we see starting on line 27. This is a pretty basic function that changes the length of the excerpt. By default it's set at 55. With the function new_excerpt_ length, we are changing that value to 40.
Right after we define the new function, we call the add_filter to hook it into place. add_filter takes two arguments: the first argument names the WordPress function, while the second specifies the custom function that's going to filter it. The same basic concept can be applied to our second custom function new_excerpt_more, which changes the output at the end of an excerpt from being a bracketed unlinked ellipsis to, in this case, a linked phrase read more. Again, the add_filter function on line 36 names the built-in WordPress function excerpt more and then the new function that's going to filter it, new excerpt more.
An action hook is different from a filter hook in that the function is triggered in response to a particular event, like a post being published for example, as opposed to changing how data is presented, which is what filter hooks do. We don't currently have any action hooks in our functions.php file, so let's add one. If you've ever looked at the code for a homepage generated by WordPress, you'll notice that WordPress by default injects a good number of meta-type code in the head section.
Not all of them, in my estimation, totally necessary. Let me show you what I mean. Let's go over to our index.php from the site root. We are in Live view and I want to enter into live code now. Then here's a perfect example right up top, a wlwmanifest-type link, and then right after it is the meta tag with the generator. Well, I think I want to get rid of both of those. So let's create an action hook that will remove them.
Go back to our functions.php page, and I'll go right after the last add_filter, staying within the PHP block, Tab in, and start my new function with the keyword function, and then give it a name remove_header_info, put our opening and closing curly braces, and then in the middle of it I'm going to use another WordPress function called remove_action.
This takes two arguments. The first argument is the name of the WordPress function that inserts the code. In this case, it's wp_head, which we put in single quotes. Then after the closing single quote put a comma to enter your second argument. This is the code that you want to remove. All of the names like this are listed for you in the WordPress Codex. So I'm typing in wlwmanifest_link. And let's close our parentheses and put a semicolon.
We are going to repeat that same line. Very similar. So remove_action(wp_head), followed by the code we want to remove, which is wp_generator. Close off our parentheses and semicolon. And I would just like filter hooks have an add_filter to activate them, we have an add action function that we need to bring in. Within its parentheses, again, there are two arguments. The first is what is the trigger for this action? And the trigger for this is a function called init, and it's encased in single quotes, followed by a comma. And the second argument is the name of the function that should be added.
Of course, it's going to be our new custom function name remove_header_info. All right, we will close our parentheses and semicolon and save our page. Now let's head over to index.php. Here are the two tags that we are hoping to get rid of. Let's refresh the page and the tags are now gone. How do you know which actions or functions can act as triggers? The WordPress Codex has a great list called the Action Reference and once you go to that page, you can scroll down and you'll see just tons of available actions that you can use.
The more you investigate them, the more you'll find that filter and action hooks can take you a long way towards creating just the site you and your client are looking for.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.