Hi Betsy, I hope this explains everything!
So, are filters a subset of actions?
They are basically the same function applied to different contexts.
If you take a peek into the core, you will see that add_action is actually a wrapper function for add_filter:
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
return add_filter($tag, $function_to_add, $priority, $accepted_args);
}
it seems I could use an action to change the text of the post for instance, instead of a filter?
Actually, you would use a filter for this situation. Let’s pretend that WordPress is a Mexican restaurant and we have ordered a taco as illustrated by the following code…
<?php $taco = 'chicken'; ?>
Basically, we have no choice… we will always be served a chicken taco. However, the same expression could be stated using the apply_filters() function. It is like ordering a taco and having the server ask you “well what kind of taco would you like?”
<?php
$taco = apply_filters( 'get_taco', 'chicken' );
?>
The above code gives us access to the type of taco that we would like to have served. We can create a function which hooks into WordPress an changes the default value of $taco
.
<?php
add_filter( 'get_taco', 'my_special_order' );
function my_special_order( $content ) {
return 'shredded beef';
}
?>
By adding the above code to you theme’s functions.php file or to a custom plugin, you are able to change the value of the variable $taco
to whatever you like. Notice that the function “my_special_order” accepts one value. In this situation the value of $content
is equal to the value of the second argument passed to the apply_filters() function. In this example it would be “chicken”.
<?php
add_filter( 'get_taco', 'my_special_order' );
function my_special_order( $content ) {
return $content . ' and avocado';
}
?>
The above code appends a string to the end of “chicken” resulting in a value of “chicken and avocado”.
what would be a good example of correctly using filters while actions would be inappropriate for the situation?
Basically, there usually is no choice for a plugin or theme developer as to which method they can use. The WordPress core is filled with calls to apply_filters() and do_action(). If I need to change the value of a piece of data, I will search through the core until I locate the file where that data is defined and see if it is possible to filter it’s value.
If I need to do something at a certain time, I will look for calls to the do_action() hook. A simple example would be if I wanted to print custom css to the head of the document. I could easily add the following code to my theme’s functions.php file:
<?php
add_action( 'wp_head', 'print_my_custom_scripts' );
function print_my_custom_scripts(){
print '<style type="text/css">.red{color:#f00;}</style>';
}
?>
Notice how this time, the function did not return a value – it did something -> it printed text to the browser.
Filters change data while actions allow us to do stuff.
when should I use an action and when a filter?
You would want to use an action where you need to do something at a certain point in time… add options to the database or print css or javascript.
You should use a filter where you would want to alter the value of some data that WordPress has passed through the apply_filters() function.
do the ‘hooks’ for actions and filters overlap? -can I add a either a filter or an action to the same hook?
Actually, both filters and actions are hooks. That being said, the answer is “no”.