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 do_action function in WordPress runs all of the functions tied into a specific action hook.
Codex Reference
-
0:00
We looked a little bit before at the do_action function.
-
0:05
What this does is execute where in the code you want a hook to take place.
-
0:11
We have do_action,
-
0:12
which can take the hook as well as arguments you want passed to it.
-
0:17
do_action can take multiple hooks.
-
0:19
Or there's the function do action reference array, which will take the hook
-
0:24
and then all of the extra hooks passed as a single array into one argument.
-
0:31
If we take a look at do_action_example,
-
0:34
we could see a couple things are going on here.
-
0:37
First, we create a function called custom_footer that all
-
0:42
it has in it is something called do_action.
-
0:47
Then, in our template code, we would cut this out, come into our
-
0:52
index, and paste in our custom_footer function.
-
0:59
So now in our template, if we call this, then this code will run.
-
1:05
So, let's go ahead and comment out the rest down here and run just this.
-
1:11
[BLANK_AUDIO]
-
1:23
We could see that we're getting an error in the page here, so
-
1:27
we have to come back into our functions.php and
-
1:30
set up do_action_example so that that function will be defined.
-
1:38
So, in our index, we're echoing out the custom_footer here and we could even put
-
1:43
just an hr or something to demonstrate that we are seeing what's on the page.
-
1:48
There's just nothing there yet.
-
1:51
If we come back into our code now though and we say, add_action, my_footer.
-
1:57
So, meaning what we defined, we set up by saying do_action here,
-
2:02
we created a new action hook.
-
2:05
And here we're saying, okay, we want to tie into my_footer action hook with
-
2:10
our custom function that's going to echo out this code.
-
2:15
If we come now and look at it,
-
2:16
we could see that the Custom footer text appears here.
-
2:20
If we wrote our code in this way, this would allow somebody else to
-
2:24
easily extend, or overwrite, or change whatever that code was originally.
-
2:31
If we look back to do_action though, and we compare it with apply_filters, we could
-
2:39
see one difference is that when we run apply_filters, it takes a default value.
-
2:46
This is because WordPress needs to have some value passed into it
-
2:50
that can then be modified and outputted.
-
2:53
That's how filters work.
-
2:55
However, with actions, we're simply giving them the opportunity to
-
2:59
run whatever code it is that they need to run.
-
3:03
And by default, there is no default action or set value with the do_action function.
-
3:09
If you find yourself in a place where you're writing a plugin or
-
3:13
theme that you want other people to be able to hook into and
-
3:16
add actions, that it's important that you add this do_action code at
-
3:21
the proper places in your site or in your plugin and then document that.
-
3:27
For example, you may be building a plugin that could have additional
-
3:30
functionality added to it.
-
3:32
However, at the time you don't want to be coding it, and you feel
-
3:35
maybe somebody else could integrate with your plugin to get it to work.
-
3:40
By providing the correct actions and filters, somebody else could easily extend
-
3:45
your plugin so that you don't have to do all the work in the first place.
-
3:49
However, their job would not be possible if
-
3:52
you didn't have the do_action as well as the add_filter in the correct place.
You need to sign up for Treehouse in order to download course files.
Sign up