Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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: http://wordpress.org/support/topic/fliters-vs-actions-from-a-newbe (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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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?

share|improve this answer
    
So when exactly do these filters/actions get executed? In the beginning/middle/end of the function? Is that something I can control? By adding "hooks" am I just saying "run this function before the function ends but after the code in the function is run"? –  Edmund Jun 20 '13 at 16:52
    
@Edmund : see the edit –  s_ha_dum Jun 20 '13 at 17:12

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 or apply_filter functions. First parameter of these functions is hook name.

So for example when you see

do_action('blabla');

It means that all actions assigned to 'blabla' hook will be called.

add_filter and add_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 value chicken. And by applies filter I mean it calls all functions assigned to get_taco hook.

Your step 3. creates new function my_special_order and assigns it to get_taco hook. So whenever there is apply_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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.