I was updating the codex page example for action hooks, while playing around to get some reuseable functions done (originally for some Q over here @WA). But then i ran into a problem i wasn't aware of before: After hooking into a function to modify the output of a variable, i can't anymore decide if i want to echo the output or just return it.

The Problem: I can modify variables that i pass to a do_action hook with a callback function. Everything i modify/add with the variable is only available inside the callback function, but not after the do_action call inside the original function.

FOR YOUR PLEASURE: I reworked it to an working example, so you can just copy/paste it in some functions.php file and see/test the problem without any efforts.

Example:

Handle data function

This is the handle_data_fn. A little over documented as it should serve as a guide on how to parse and merge default with other arguments.
You can see the problem at the end of the function right after the do_action() call.

Actions aren't for modifying data, they're points in time for producing additional output, filters are for modifying data before a specific thing happens to them. If the hook wants to allow data to be changed, it's typically a filter, if the hooks wants to allow extra output to occur in a specific place, it's an action.... generally
–
t31osMar 20 '11 at 11:01

1

@t31os - I frequently use actions to modify data in global vars because there is often no other way to accomplish a specific client requirement. JMTCW.
–
MikeSchinkelMar 20 '11 at 21:33

1

@Mike - Sure you can use it for changing globals, i was just saying that generally actions aren't for changing data, though in some cases they may be.
–
t31osMar 21 '11 at 9:20

1

@t31os - Agreed. Only reason I mentioned is some people take things they read from people like you they respect as dogma. Whereas you know when to violate the guideline, many will adhere to it is as a rule.
–
MikeSchinkelMar 22 '11 at 23:49

@MikeSchinkel @t31os I upvoted both your comments. Good that this its mentioned here (that they generally aren't for changing data) below this Q. The Q/function itself is abstract enough to cause some confusion. (From a look at the core function code itself there's no real difference between do_action and apply_filters.)
–
kaiser♦Mar 23 '11 at 1:46

So you'd suggest to replace the second do_action with apply_filters? This would force me to drop the echo argument and decide if i want to use the filter or the action call, which i woul call "less elegant" too :). Further more i wouldn't know how to pass the $args and the $options into one filter and work with it. Could you update your answer & provide some (short) example? Thanks.
–
kaiser♦Mar 19 '11 at 17:45

@Bainternet: Sry, but that's no answer to the Q. That's some explanation of how hooks & filters work. I already know how the concept works. - If you want to tell me about some alternating concept: feel free to teach me.
–
kaiser♦Mar 20 '11 at 2:56

This was how i first had it, before trying to use a hook and it wasn't working. I wanted to tell you this, but i just re-tried it and it worked. I guess i had misspelled something or similar stupid stuff. So, thanks for your time and answer. +1
–
kaiser♦Mar 20 '11 at 13:50