I am about to start my first WordPress project and I have the task of moving customized core code that override the core into separate functions/files (this way it’ll be easier to maintain when we upgrade in the future.
I have been doing research and it looks like add_filter
and apply_filter
and do_action
and add_action
seem to be the way to go. I’m still unsure about how they work though. Are those methods what I use to overwrite core methods? I am confused because add_filter
uses the word add
when I feel like it is more on the lines of replace
or overwrite
(unless I am misunderstanding)
I just read the first answer to this (the one about the tacos)
So there were three steps the author used in describing how to use add_filter:
Step 1: a static value for the $taco variable
<?php $taco = 'chicken'; ?>
Step 2:
<?php
$taco = apply_filters( 'get_taco', 'chicken' );
?>
Step 3:
<?php
add_filter( 'get_taco', 'my_special_order' );
function my_special_order( $content ) {
return 'shredded beef';
}
?>
So in this situation is the apply_filter
just creating a dynamic method on the fly and making it return the chicken
string? Or is the assumption here that there is a core WordPress method called get_taco
and that using apply_filter
would help in overriding whatever the original functionality was?
Also, can you only apply_filter
something that you’ve add_filter
ed? In other words, would apply_filter('get_taco');
not work if I did not define add_filter('get_taco', 'my_special_order');
AND function my_special_order ($content) ...
first?
Do I put these filters in my functions.php file? Sorry for bombarding you with questions, I am coming from Ruby on Rails so I’m completely clueless in WordPress.
You are misunderstanding. Both
add_action
andadd_filter
insert function callbacks into a kind of queue. You can add many callbacks to the same hook and they will fire in the sequence added except when a priority is included as the third parameter.apply_filters
— note the ‘s’– does more or less create the filter but usually it is passed a variable not a string and that variable is passed to the callback which then alters it and returns it.Filter ‘slugs’ are not the names of WordPress Core functions or methods as a rule though some functions have filters of the same name. For example,
the_content()
calls thethe_content
filter.Hooks do not usually override whole function, only parts of them, but there are exceptions and near exceptions.
No. The other way around. You can only
add_filter
something that isapply_filter
ed. You can useadd_filter('whatever','abc');
but nothing at all will happen if there is not anapply_filter('whatever',...
somewhere.Pretty much the same applies to
add_action
/do_action
but actions are for doing things rather than returning values.Hooks are executed at various times in the page load and at various places in functions and methods. For example,
save_post
executes near the end ofwp_insert_post
whilepre_get_posts
executes near the top of theget_posts
method of theWP_Query
class. Others execute in the middle of the page load and not in a function or method at all. They execute where ever there is a need (as determined by the developers) to either alter or add information, or run other functions, redirect a page, etc.More Information
Difference Between Filter and Action Hooks?
I guess you’ve got it a little bit wrong.
Filters and actions are functions that you “assign” to hooks. In many places WordPress calls
do_action
orapply_filter
functions. First parameter of these functions is hook name.So for example when you see
It means that all actions assigned to ‘blabla’ hook will be called.
add_filter
andadd_action
does not overwrite anything. It does exactly what it says – it assigns your function to given hook.So your Step 2. applies filter
get_taco
to static valuechicken
. And by applies filter I mean it calls all functions assigned toget_taco
hook.Your step 3. creates new function
my_special_order
and assigns it toget_taco
hook. So whenever there isapply_filter('get_taco')
call, your function will be called.If there is no filter/action assigned to given hook, nothing will be done.
You can read more on this topic here: http://codex.wordpress.org/Plugin_API