There are two types of wordpress hooks, actions and filters.
In this video, we'll focus on filters,
but check out the actions lesson
for more information on action hooks.
Fultors allow you to modify or filter some data
at a specific point, which will be used later on.
In order to make use of a filter,
you register a function in your code
to a pre-existing filter hook,
which is known as a callback function.
To explain this, let's look at a filter called
the content.
This filter is defined in the WP includes
post-template.php file,
inside the function, which is used in theme templates
whenever the template needs to render any post or page content.
Inside that function, we see this line of code.
Here, the apply filters function defines the filter hook,
with the hook name of the content.
You will notice that a content variable is passed
as an argument of apply filters.
And that the value of apply filters
is assigned back to a variable.
In this case, again, the content variable.
If we look a little higher in this function,
the content variable is assigned
from the value of the get the content function,
which is a WordPress core function
that retrieves the value of the post-content field
for the current postal page in the post table.
So the apply filters function registers the filter hook,
passes the value of content at this point
in the code execution to any callback functions
registered on this hook,
and requires the updated value to be passed back.
If still your callback function on a filter,
you use the WordPress add filter function.
You will need to pass the hook name
and the name of your callback function
as arguments to the add filter function.
Let's take a look at what this looks like
in a themesfunctions.php file.
For this lesson, you're going to write some code
inside your currently active theme.
Please note that this is for demonstrative purposes only.
Unless you are developing the theme yourself,
it is not recommended to edit
an existing themes files directly.
In your code editor, navigate to your currently active themes
functions.php file and open it.
If your theme doesn't have a functions.php file,
you can create one in the root of your theme directory.
Just make sure it's named functions.php
and has the opening PHP tag at the top of the file.
Then, add the following code to your functions.php file
to hook the callback function into the content full-took.
Add filter, the content, and the callback function
is WPLearnAmmandContent.
Then create the callback function
using the PHP function syntax
and make sure it accepts the relevant argument from the filter.
accepts the relevant argument from the filter.
You don't have to name the argument the same as the variable name passed from the filter,
but it does make it easier if you do.
You also have to return the updated data.
This is so that the original variable being updated from the apply filter score gets
the updated data.
For example, let's say you wanted to add something to the end of the content of each
post.
You could append it to the content variable like this.
First, create an additional content variable and then append it to the content variable.
In this example, you're adding some text in a paragraph block, which renders the bottom
of each post on the front end.
It's very important to always return something back from a filter, ideally the modified content
of the variable passed to the filter.
Without returning something, we'll cause a fatal error on your WordPress site.
Let's take a look at what this looks like in our WordPress site.
If you view any post or page on the front end, you will see the text filtered through
the content at the bottom of the content.
So as you can see from this example, filter hooks allow you to modify certain pieces of
data at a specific point in the code execution.