[wp-hackers] add_filter() with multiple arguments

Michael D Adams mda at blogwaffe.com
Mon Aug 23 17:26:22 UTC 2010


On Mon, Aug 23, 2010 at 10:11 AM, Eric Mann <eric at eamann.com> wrote:
> It's fairly easy to add a filter that accepts multiple parameters, but how do
> you return those parameters?

In general, you cannot.

> Case-in-point:
> http://wordpress.stackexchange.com/questions/741/how-can-i-filter-children-of-the-uncategorized-category-out-of-the-loop
>
> The solution to this particular category problem is to add a filter to
> 'get_categories_taxonomy' which passes two arguments, $args['taxonomy'] and
> $args.  But what do you pass back in the return?

Try the 'list_terms_exclusions' filter instead.  It's not pretty (you
have to modify SQL), but it should work.


> Will:
>
> function exclude_children($taxonomy, $args) {
>     $args['exclude_tree'] => '1';
>     return $args;
> }
>
> add_filter('get_categories_taxonomy', 'exclude_children', 10, 2);
>
> break anything when it executes?

Yes, because in the get_categories() function, $taxonomy is now set to
$args.  Try it and see :)  Add a Categories widget to your theme, and
load that code in a plugin.

>  I've never added a filter that accepted
> multiple arguments before, and the Codex documentation only addresses receiving
> more than one argument, not passing them back ...

You can't return multiple arguments.  You can modify multiple
arguments iff the hook was called with do_action_ref_array() or
apply_filters_ref_array().

Mike
--mdawaffe

PS: Don't use categories at all for the above.  Register a custom taxonomy.


More information about the wp-hackers mailing list