Using Filters

Adding and removing functions to/from hooks

# It always works the same when you are adding and moving stuff. you add FUNCTIONS to HOOKS. think of hooks like parking spots. some are empty, some have cars on them. you can move the cars around, etc.

function function_you_want_to_add(){
echo "I heart bacon!";
}
add_action('destination_hook','function_you_want_to_add',$priority# );

Removing stuff works a bit differently:

function remove_stuff(){
remove_action('hook_location','function_you_want_to_remove',$priority# );
}
add_action('init','remove_stuff');

init is just a default wordpress hook. in fact, it is the FIRST one that runs when WP starts whirring.

Using filters

it took me a long time to understand filters. think of filters like this… they are still parked in the same parking spot, but something about the car is different. filters change something w/o moving it.

you can filter anything that says

apply_filters('this_is_the_filter_name', $somevariable);

filters take something IN and spit something OUT. send a blue car into the function and it might come out red.

function sample_filter_function($input_from_original){
$send_output_to_filter = $input_from_original . " Add some bacon!";
return $send_output_to_filter;
} add_filter('this_is_the_filter_name','sample_filter_function',$priority#);

Whatever content WAS going to be printed will now have “ Add some bacon!“ tacked on to the end, b/c well.. who couldn’t use more bacon?

Lastly, the $priority# stands for priority number. it is like a traffic cop in the parking lot. or maybe just an orange cone. if more than 1 function wants to be on a particular hook the priority decides which goes first same as if 2 cars want to park in the same spot (sorry, had to beat this metaphor to death). the higher the number the later it gets run. this can be important, but is not always necessary.

Props to @kwight for starting the parking metaphor. Apparently my ball-pit metaphor didn’t make any sense b/c no one knew what a ball-pit was.