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 not able to understand a very logic behind add_action / add_filter

Consider following example:

function SearchFilter($query) {
  if ($query->is_search) {
     $query->set('post_type', 'page');
  }
  return $query;
}
add_filter('pre_get_posts','SearchFilter');

My questions are

  • How can I know wheter function 'SearchFilter' accepts any argument or not?

  • What will be that argument / what will be it's type?

  • How can I determine for which action/filter there is any argument?

  • Should every action / filter return the passed parameter?

share|improve this question

2 Answers 2

The best way I know of to get args is to look up the do_action call for the hook you're using in the code. adambrown.info has a great section on this. Here's the one you'll need.

share|improve this answer
    
I am still confused. I am not able to understand how many parameters should be accepted by function, and what will be those arguments? –  I-M-JM Jan 10 '12 at 10:24
    
Does the documentation on do_action() and do_action_ref_array() help you at all? I'm not sure which part of the code you're not understanding. –  m0r7if3r Jan 10 '12 at 10:53

When calling an action hook using

add_action('some_action_tag','callback_function',$priority,$number_of_arguments);

The callback function is called when a do_action() call is fired with the corresponding tag for example:

do_action('some_action_tag',$argument1,$argument2,$argument3....);

And when calling a filter hook using:

add_filter('some_filter_tag','callback_function',$priority,$number_of_arguments);

The callback function is called when a apply_filters() call is fired with the corresponding tag for example:

$value = apply_filters('some_filter_tag',$argument1,$argument2,$argument3...);

Now as you can see the main difference is that the action hook basically lets you run your own function at a given point,, and a filter hook lets you filter (change/alter/clean ...) the value of a specific variable.

So an action "hooked" callback_function accepts arguments only if any were specified and a filter "hooked" callback_function accepts at least one argument which is the value to alter itself and more arguments only if specified.

As for you questions

How can I know whether function 'SearchFilter' accepts any argument or not?

The hooked function (in this case SearchFilter) accepts whatever arguments that the hook trigger (either apply_filters() for filter hooks or do_action() for action hooks) is passing to it and the best way to know what they are is to search the code for the hook tag (e.g searchFilter) and see what they are. However as stated above since we you are hooking it to a filter hook it will have at least one argument.

What will be that argument / what will be it's type? How can I determine for which action/filter there is any argument?

Same as above you can see that in the hook trigger function or by creating a test dump which would be something like this?

function test_dump($a1=null,$a2=null,$a3=null,$a4=null,$a5=null){
    if ($a1 !== null){
        echo '<pre>first argument: <br/>';
        var_dump($a);
        echo '</pre>';
    }
    if ($a2 !== null){
        echo '<pre>2nd argument: <br/>';
        var_dump($a);
        echo '</pre>';
    }
    if ($a3 !== null){
        echo '<pre>3rd argument: <br/>';
        var_dump($a);
        echo '</pre>';
    }
    if ($a4 !== null){
        echo '<pre>4th argument: <br/>';
        var_dump($a);
        echo '</pre>';
    }
    if ($a5 !== null){
        echo '<pre>5th argument: <br/>';
        var_dump($a);
        echo '</pre>';
    }
}

Should every action / filter return the passed parameter?

Action hooks doesn't expect a return value so no, but a filter hook should return a value almost every time.

share|improve this answer
    
This anwer is gold! –  Maxwell Aug 17 '13 at 17:32

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.