Make WordPress Core

Opened 17 years ago

Closed 15 years ago

Last modified 15 years ago

#6283 closed defect (bug) (invalid)

apply_filters() bug

Reported by: denis-de-bernardy's profile Denis-de-Bernardy Owned by: westi's profile westi
Milestone: Priority: normal
Severity: normal Version: 2.5
Component: General Keywords: needs-patch
Focuses: Cc:

Description

the documented arguments are:

add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1)

I would expect, upon announcing that zero arguments are involved in a function, that wp would simply call it and move on with life.

I get this strange result, however:

function foo() { /* do something, return nothing */ }

add_filter('the_content', 'foo', 0, 0);

var_dump(apply_filters('the_content', 'bar'));

returned result:

expected result: the_content filtered 'bar' as if foo had not been called

Change History (9)

#1 @lloydbudd
17 years ago

Denis-de-Bernardy, not sure how to convince you to include the revision # you find the bugs in :(

#2 @westi
17 years ago

  • Keywords reporter-feedback added
  • Milestone changed from 2.6 to 2.7
  • Owner changed from anonymous to westi
  • Status changed from new to assigned

Reading through this doesn't make much sense.

Are you saying that you expect apply_filters(...) to return something here?

You have added a filter to the_content at priority 0 i.e. it's going to be called first. You are also saying that your filter effectively doesn't return anything. Therefore any later filters are going to get no input so the overall result is going to be nothing.

Any useful filter must return something useful as the result of one filter is passed to the next until they run out.

#3 @pishmishy
16 years ago

  • Milestone 2.7 deleted
  • Resolution set to invalid
  • Status changed from assigned to closed

Resolving as invalid. Westi is correct. You might, at some stage, want to define a filter that completely overrode the input

function foo() {
    return "I've overiden the content of this post!";
}

To get your expected result foo should be defined as:

function foo($arg) {
    return $arg;
}

#4 @Denis-de-Bernardy
15 years ago

  • Keywords needs-patch added; reporter-feedback removed
  • Milestone set to 2.9

no no, it's not invalid.

I initially ran into this one while playing around with caching functionalities. Basically, I wanted some caches to get flushed on various hooks in WP that were called by apply_filters() rather than do_action(). Since then, I've "fixed" this by sticking to having an $in = null variable on cache flushing functions, and returning that $in at the end of the function.

Still, if the function is announced as having zero arguments, the apply_filters function should probably ignore whatever it returns (if anything). Or then the documentation needs to be clarified.

It's an arguably minor defect, but it's definitely a confusion one when you initially run into it.

#5 @Denis-de-Bernardy
15 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

#6 follow-up: @DD32
15 years ago

Just because a filter accepts 0 vars, Doesnt mean that the function doesnt return data.

That being said, Why is the $accepted_args even there? AFAIK, You can call a function with extra vars without issue. (ie. function test(){ func_get_args() } test(1,2,3,4);) infact, func_get_args() is used commonly by WordPress..

Suggest invalid myself again, Its not a defect, Its a feature request. Filters work by filtering data, Actions work as acting upon an event occuring, Thats what they're designed as/for.

#7 in reply to: ↑ 6 @Denis-de-Bernardy
15 years ago

  • Resolution set to invalid
  • Status changed from reopened to closed

Replying to DD32:

That being said, Why is the $accepted_args even there? AFAIK, You can call a function with extra vars without issue. (ie. function test(){ func_get_args() } test(1,2,3,4);) infact, func_get_args() is used commonly by WordPress..

actually, this only works with functions you define. if you try it with php's built-in functions, you'll get a warning.

closing as invalid then.

#8 @DD32
15 years ago

if you try it with php's built-in functions, you'll get a warning.

Hm.. True, How annoying.

#9 @Denis-de-Bernardy
15 years ago

  • Milestone 2.9 deleted
Note: See TracTickets for help on using tickets.