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.

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!

share|improve this question
    
Method 1 is completely valid. Have you tried it yourself? Method 2 will not work, since add_filter does not allow you to pass additional arguments. –  soulseekah Oct 19 '11 at 5:57
    
Yes... I tried the first one, it works! Thanks! –  Rutwick Gangurde Oct 19 '11 at 5:58
    
For method 2, you can wrap a custom filter over a defined filter and conditionally have it pass a second argument, I think. But I don't see any reason to other than for education. –  soulseekah Oct 19 '11 at 6:01
    
Know a place where I can see an example? Like a blog or a tut? –  Rutwick Gangurde Oct 19 '11 at 6:03
    
Education it is then... check my answer :) –  soulseekah Oct 19 '11 at 6:17
add comment

1 Answer

up vote 3 down vote accepted

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:

function fnc_1( $content ) { return 'func1 :: '.$content;   }
function fnc_2( $content ) { return 'func2 :: '.$content;   }
function fnc_3( $content ) { return 'func3 :: '.$content;   }

$a = false;
$b = false;

if ( $a ) add_filter( 'the_content', 'fnc_1' );
elseif ( $b ) add_filter( 'the_content', 'fnc_2' );
else add_filter( 'the_content', 'fnc_3' );

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:

add_filter( 'wpse31470_filter_wrapper', 'wpse31470_filter_wrapper_func', null, 2 );

function wpse31470_filter_wrapper_func( $content, $condition ) {
    if ( $condition ) return 'TRUE :: '.$content;
    else return 'FALSE ::'.$content;
}

add_filter( 'the_content', 'wpse31470_the_content_filter' );
function wpse31470_the_content_filter( $content ) {
    $condition = true;
    return apply_filters( 'wpse31470_filter_wrapper', $content, $condition );
}

The purpose of the wpse31470_the_content_filter is to wrap the argument supplied by the_content filter and pass it on to your own wpse31470_filter_wrapper along with any additional arguments.

share|improve this answer
    
Awesome! Thanks a lot... I'm trying the 2nd apply_filters method right away, cause if it works, many of my workarounds will go away! +1! –  Rutwick Gangurde Oct 19 '11 at 6:35
    
Glad to have been of help. Let me know if you stumble upon any questions or suggestions. And don't forget to improve your accept rate (67% is low) in order to get more quality answers in our community wink –  soulseekah Oct 19 '11 at 6:46
1  
Yes indeed! If I stumble, I won't hesitate to ask you! And there goes the 'accept' ;) –  Rutwick Gangurde Oct 19 '11 at 6:49
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.