Is it possible to hook 3 different functions to the same filter conditionally, like:
<?php
if(a){
add_filter('the_content', 'fnc_1');
}
else if(b){
add_filter('the_content', 'fnc_2');
}
else{
add_filter('the_content', 'fnc_3');
}
?>
Or if this isn’t possible, then can I pass an additional argument to the same function to flag my 3 different conditions? Something like:
<?php
if(a){
add_filter('the_content', 'fnc_1', 'condition_a');
}
else if(b){
add_filter('the_content', 'fnc_1', 'condition_b');
}
else{
add_filter('the_content', 'fnc_1', 'condition_c');
}
?>
All I could find from my reading is that it has to do something with apply_filters
, but couldn’t get around it.
Please can anyone tell me how to achieve this?
Thanks!
Since it does appear to be an interesting question, I’ll go ahead an compile an answer.
Method 1
Totally fine, will work. Here’s the compact piece of code I used to test it:
Method 2
add_filter
does not allow you to pass additional arguments to the filter function, but if you really need to share one function you can wrap a custom filter around the filter like so:The purpose of the
wpse31470_the_content_filter
is to wrap the argument supplied bythe_content
filter and pass it on to your ownwpse31470_filter_wrapper
along with any additional arguments.