Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
The apply_filters function is used in the WordPress source code or plugin code to apply filters. In this video we show how to use apply filters as well as show again how to use it to search through WordPress Core to look for where filters are applied.
Code Reference
-
0:00
Let's take a look now at the all-important apply filters in a little bit more depth.
-
0:05
If we pull up the reference, we can see that it's taking a few parameters here.
-
0:10
It's taking the name of the filter that we wanted to have.
-
0:15
And then it's taking the default value.
-
0:19
And finally, one or
-
0:20
more variables passed that we could use within the filter functions.
-
0:27
If we come down and
-
0:28
take a look we can see that it is possible to pass multiple arguments through.
-
0:34
If we look up here, we can also see in addition to apply filters.
-
0:38
We also have apply filters reference array.
-
0:42
The difference is, is that all of the extra parameters from default value
-
0:47
to the extra arguments as well, can all be written as a single array and
-
0:52
then passed into apply_filters_ref_array.
-
0:56
If we're just using apply filters and we want to use multiple variables,
-
1:01
then we would have to list them out one after another.
-
1:05
Lets take a look at an example just using a new hook and an example default value.
-
1:12
If we open up applyfilters.php inside of our examples.
-
1:17
We have two pieces of code going on.
-
1:19
The top part is where we are writing a new function that's tying into
-
1:25
our existing example_filter.
-
1:27
Later on down in the code, we're running apply_filters.
-
1:32
You need these two components to work.
-
1:35
This bottom section of the code is what you would add to your own plugin or
-
1:39
theme, whatever it was that you wanted to grant other people access to
-
1:44
filter piece of information.
-
1:46
So, what you would do in your code, is use apply_filters.
-
1:51
Give the name of the filter that you want to create.
-
1:55
So your name here would be whatever it is that you want it to be named.
-
2:01
And then you pass it the default value.
-
2:04
This could either be a variable or
-
2:06
in this case, a string as we're doing it like this.
-
2:11
And in this case, we're not gonna pass any variables to it.
-
2:14
We'll just do a simple one like this.
-
2:17
So apply filters.
-
2:19
We're naming our filter here and then we're giving it a default value to use for
-
2:23
our app in case nobody else is hooked into it.
-
2:26
Then, this code up here is going to create a function called example_callback and
-
2:32
hook that in at priority ten, which is default so if we're gonna set it to ten,
-
2:37
we could simply remove it as well to make our code a little bit cleaner.
-
2:41
It's going to pass in the default value.
-
2:44
And then we're going to echo it out or return something new.
-
2:49
We could also do something like this.
-
2:51
[SOUND] And then return
-
3:01
the new value.
-
3:08
Whatever it was, whatever edit it is that we want to make.
-
3:13
So lets come into our functions page dot page p and
-
3:16
on top of list hooks we're going to apply the apply filters file.
-
3:25
So now when we refresh our page let's see what we have going on here.
-
3:32
Sorry.
-
3:33
That would be JavaScript.
-
3:35
Okay, so what we have here is default value new displaying.
-
3:40
Which is what we would expect it to based on this code.
-
3:43
But let's back it up and look at something else that happens.
-
3:47
Which is let's say that we write our code, and
-
3:50
in our code we're giving people the opportunity to add a filter to something,
-
3:55
but we're not using it ourselves.
-
3:57
So lets say that we never wrote a code that ties into our own filter.
-
4:01
We simply set the default value and said okay go ahead and
-
4:06
echo this out if there are filters run em, if not just give us default value.
-
4:10
So what we would expect to see is default value.
-
4:14
However, if we uncomment this.
-
4:17
We should see this default value being passed as a variable,
-
4:23
us using that variable to reset something new and then returning it.
-
4:28
So now we could see it show up.
-
4:30
The other cool thing is that because this WP filters array contains all of
-
4:35
the hooks in Word Press, because we created and
-
4:38
added a new hook, we should be able to search.
-
4:41
And see it show up along with the function that we've tied in to it.
-
4:47
But if we change our code and comment this out,
-
4:50
whereas we're using the apply_filters but no functions are trying to hook into it.
-
4:56
If we refresh our page and look for our hook now, we won't see it show up.
-
5:01
This is important to note that a filter can have a potential, meaning that
-
5:06
somewhere in the code they could be giving you the options to apply a filter.
-
5:10
However, until somebody actually adds something to it
-
5:15
it won't show up in this list.
-
5:18
What that means is that you may have a plugin or
-
5:20
some extra code that may have some filters that you're not seeing here
-
5:24
because they're not necessarily being called or having anything tied to them.
-
5:30
However, this is a good overview of how apply filters work and
-
5:34
you should be able to do this in your own code.
-
5:36
By copying in the relevant apply_filters where you need it, and then either using
-
5:42
it yourself, or documenting it so that other people can tie into your code.
You need to sign up for Treehouse in order to download course files.
Sign up