Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Hooks - Actions and Filters Action Functions in WordPress The do_action Function

Fernando Cortes
Fernando Cortes
2,435 Points

Why do we need an action hook at all?

Couldn't we just create this function:

function custom_footer_text() {
  echo "Custom footer text";
}

and then simply call it from index.php with:

custom_footer_text();

I'm sure I'm missing something, but I just don't see why you need do_action to create the hook and then add_action to tie it to the custom_footer_text function.

Is it a matter of scope? Would calling the function from index.php not give WordPress enough information to find where the function is?

2 Answers

Fernando Cortes
Fernando Cortes
2,435 Points

Thanks Jake.

I've started to glimpse possibilities like those you mention, but I think I still need to do a lot of tinkering with hooks for the whole concept to sink in.

Jake Flaten
Jake Flaten
5,910 Points

truthfully, you could accomplish the same thing that we're doing in this example by going into your index.php and putting: <?php echo "Custom footer text" ?>

This video is just showing us how the do_action function works on a very,very simple level. I believe you'd probably want to use the do_action function(main) in a place where you you had a function with a lot of other functions hooked into it, but that function(main) itself isn't hooked into a function, and thus would never run unless you told it to with the do_action function.

Another place where one could use the do_action is if you had an action with a lot of hooks that you wanted to use in different specific pages, say an archive and a portfolio page. you could use the do_action in the templates for both of these while you have the defintion for these in the functions.php file which is where it should be.