Clarification on filters and hooks

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_filtered? 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.

2 s
2

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_filtered. 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.

More Information

Difference Between Filter and Action Hooks?

Leave a Comment