Make WordPress Core

Opened 14 years ago

Closed 14 years ago

#12723 closed defect (bug) (fixed)

apply_filters_ref_array bug

Reported by: jfarthing84's profile jfarthing84 Owned by: westi's profile westi
Milestone: 3.0 Priority: highest omg bbq
Severity: blocker Version: 3.0
Component: Plugins Keywords: has-patch tested commit
Focuses: Cc:

Description

I believe (unless I am not using it correctly) that there is a bug with the new apply_filters_ref_array() function. Particularly, I am applying two different functions certain 'posts_' filters in the query, and only the one with the highest priority is processed. Small example:

function posts_where_filter_one($where) {
  global $wpdb;
  $where .= " AND $wpdb->posts.post_type = 'post'";
}
add_filter('posts_where', 'posts_where_filter_one');

function posts_where_filter_two($where) {
  global $wpdb;
  $where .= " AND $wpdb->posts.post_status = 'publish'";
}
add_filter('posts_where', 'posts_where_filter_two');

You would expect $where to end up as:

AND wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish'

But instead it end up as:

AND wp_posts.post_status = 'publish'

Attachments (1)

12723.diff (982 bytes) - added by scribu 14 years ago.

Download all attachments as: .zip

Change History (15)

#1 @scribu
14 years ago

  • Milestone Unassigned deleted
  • Resolution set to invalid
  • Status changed from new to closed

Erm, you forgot to do return $where in both cases.

#2 @scribu
14 years ago

  • Milestone set to 3.0
  • Resolution invalid deleted
  • Status changed from closed to reopened

On a closer look, bug confirmed.

#3 @scribu
14 years ago

  • Keywords dev-feedback removed
  • Owner changed from westi to scribu
  • Status changed from reopened to accepted

@scribu
14 years ago

#4 @scribu
14 years ago

  • Keywords has-patch needs-testing added

#5 @jfarthing84
14 years ago

Huh, oops. I sure did. I didn'tforget in my code though, just on typing the example here.

#6 @scribu
14 years ago

jfarthing84, can you confirm that the patch fixes the problem?

#7 @jfarthing84
14 years ago

Yes, it does.

#8 @scribu
14 years ago

  • Keywords tested commit added; needs-testing removed

Good enough. :)

#9 @scribu
14 years ago

  • Owner changed from scribu to westi
  • Status changed from accepted to assigned

#10 @westi
14 years ago

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

Your filters are not written correctly.

A filter hooked into a WordPress filter must return something to be passed back out.

By default the first argument passed to apply_filters / apply_filters_ref_array is returned unless a function is hooked in.

If something is hooked in then the result of that function is returned instead.

If there are a number of filters then the result of one gets passed to the next as an argument.

Your examples should be:

function posts_where_filter_one($where) {
  global $wpdb;
  $where .= " AND $wpdb->posts.post_type = 'post'";
  return $where;
}
add_filter('posts_where', 'posts_where_filter_one');

function posts_where_filter_two($where) {
  global $wpdb;
  $where .= " AND $wpdb->posts.post_status = 'publish'";
  return $where;
}
add_filter('posts_where', 'posts_where_filter_two');

Closing as invalid

#11 @jfarthing84
14 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

Wow, if you look up I said that I just forgot to type the return in my mockup examples here, not in my code. It is a bug. Try it yourself.

#12 @jfarthing84
14 years ago

  • Milestone set to 3.0

#13 @westi
14 years ago

Ok I missed the subtlety of the bug and the need for multiple filters.

Sorry.

I've added a test case for this now to wordpress-tests so it won't reappear in the future.

#14 @westi
14 years ago

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

(In [13906]) Ensure that apply_filters_ref_array passes the result of the filter to the next filter. Fixes #12723 props scribue.

Note: See TracTickets for help on using tickets.