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)
You are misunderstanding. Both add_action
and add_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.
So in this situation is the apply_filter just creating a dynamic
method on the fly and making it return the chicken string?
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 the the_content
filter.
Hooks do not usually override whole function, only parts of them, but there are exceptions and near exceptions.
Also, can you only apply_filter something that you’ve add_filtered ?
No. The other way around. You can only add_filter
something that is apply_filter
ed. You can use add_filter('whatever','abc');
but nothing at all will happen if there is not an apply_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 of wp_insert_post
while pre_get_posts
executes near the top of the get_posts
method of the WP_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.