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 understand that using add_filter() or remove_filter() directly within a themes' functions.php would make the changes global across the theme unless you override them elsewhere. But believe this is a result of WordPress initialization.

add_filter( $tag, $function_to_add, $priority, $accepted_args );

However if a call to add_filter() or remove_filter() is made within a function or class:

function example(){
    ...
    add_filter( $tag, $function_to_add, $priority, $accepted_args );
    ...
}

What is the scope and persistence of the change?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

It is always global. add_filter() and add_action() are just wrapper for the global variable $wp_filter. So it doesn’t matter where the function is called.

The same is true for apply_filters(), apply_filters_ref_array(), do_action_ref_array() and do_action(): they work in the global namespace, that’s the reason why the callback handlers (functions or methods) have to be public. A protected class method cannot be called from the global scope.

share|improve this answer
    
Thanks for the clarification and the much needed Homer Simpson moment. –  Stephen Nov 4 '12 at 13:25
add comment

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.