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!

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 Filter Functions in WordPress The remove_filter Function

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,250 Points

So simply speaking about Hooks and Filters

If I have this right, Hooks are little functions in Wordpress so you can attach code or actions into a specific part of a Wordpress site.

You have statements in function.php like..

require_once('examples/export_wp_filter.php');

to pull in the code that makes a particular filter work.

You have the code used to tie a function into a hook in a file such as add_filter.php.

<?php add_filter( $tag, $function_to_add, $priority, $accepted_args ); ?>

And finally a php file for the function that gets tied into the hook to... such as

<?php
 function my_custom_excerpt_length($length) {
  return 20;
}

?>

Have I got this right? I'm struggling to get my head around this :)

Thanks!

1 Answer

Brian Hayes
Brian Hayes
20,986 Points

First of all, require(), require_once(), include(), and include_once() are actually PHP functions meant for importing other php files. This has nothing to do with any WordPress functionality, but still needed to bring in files that contain functions and such.

now, there is a difference in how these functions bring in the code from a file however. For starters the difference between require and include is that the require functions will throw a fatal error if the file cannot be found/imported, and the include function will just give off an error, but allow the page to continue to load. That extra "once" on the end of each of those functions means that the file will only ever be brought in one time. In a lot of cases I use include_once(), unless I have a good reason for a failure of the importation to break my page, then I'll use require_once().

I have yet to run into a situation where I actually had a true need to simply use require() or include(). Things tend to still work, but to save future errors i use require_once() and include_once().

As for Action and Filter hooks, think of action hooks as a way to DO something and filter hooks as a way to MODIFY something.

so, for instance, to enqueue a script I hook a function into an action hook called wp_enqueue_scripts. I'm basically telling WordPress to execute my function that contains the function that outputs the proper markup when that hook loads.

but a filter, I'm intercepting a function and modifying its arguments before it executes. So like in your example, you would be modifying the value of the $length variable that exists in the function you are filtering with your own function that you have hooked into it with.