[buddypress-trac] [BuddyPress] #3985: Race condition processing Ajax requests.

buddypress-trac at lists.automattic.com buddypress-trac at lists.automattic.com
Fri Mar 30 18:59:21 UTC 2012

#3985: Race condition processing Ajax requests.
 Reporter:  gary_mazz     |       Owner:
     Type:  defect (bug)  |      Status:  reopened
 Priority:  normal        |   Milestone:  1.6
Component:  Core          |     Version:  1.5.3
 Severity:  major         |  Resolution:
 Keywords:  dev-feedback  |
Changes (by MikeLittle):

 * cc: MikeLittle (added)
 * status:  closed => reopened
 * resolution:  fixed =>


 I do not believe this is the correct solution to this problem. Let me give
 my example:

 I have a plugin that registers some custom taxonomies for users. This
 plugin is not dependent on BuddyPress, but if BuddyPress is active, it
 adds some more hooks (on plugins loaded, because bp_include won't work in
 the middle of a class!) to display these user taxonomies on the profile
 page, for example.

 A colleague decided to highlight one of the user's taxonomies on the
 members page. It works fine on first view of the members page and
 correctly display one of the custom taxonomy terms for each user. But when
 using the order by drop down and triggering an ajax refresh, it stops

 On investigation, I found that the taxonomies have not been created by the
 time the template code runs again!

 The taxonomies are created on the 'init' hook at the default priority
 level. The problem comes because bp_init(), the function, is also hooked
 on 'init' at the default level. The final piece in this problem is that
 bp_core_add_ajax_hook() is hooked on bp_init too.
 So here's the bad scenario.

 plugin 1 hooks function_1() on init
 buddypress hooks bp_init() on init, and thus hooks bp_core_add_ajax_hook()
 plugin_2 hooks function_2 on init

 on a normal request everything is fine: all the functions hooked on init
 run long before any template files are displayed.

 On an ajax request though, function_1() runs, then bp_init() runs, bp_init
 it calls do_action(bp_init) which calls the ajax_handler. That loads a
 template file, and starts generating some html, which wants to use the
 taxonomies registered by function_2().
 Poor old function_2() hasn't run yet!, because WordPress hasn't finished
 processing all the init hooks yet!

 Function_2 is my function which defines the taxonomies and is sensibly
 hooked on init.  (register_taxonomy() should not be called before init
 according to it's docs)

 Now, I can work around the problem by hooking my taxonomy creation at a
 higher priority than bp_init. But that ''is'' a work-around.

 The problem is you should not be calling locate_template or even ajax
 handling from a default priority 'init' hook, it's way too early.

 Try hooking the ajax handler on 'wp_loaded' if you can't, for some reason,
 use the proper WP ajax handling (wp-admin/admin-ajax.php).

 In fact right at the bottom of wp-settings.php just after it calls
 do_action('init'), it recommends using the proper ajax handling, then
 suggests (implies) you can used the wp_loaded action for such things.

 So, please, the correct solution to this problem, and other problems I've
 seen around non-initialisation during ajax processing, is to move the ajax
 processing to the proper admin-ajax.php processing. OR failing that, shift
 it to the 'wp_loaded' action.

Ticket URL: <https://buddypress.trac.wordpress.org/ticket/3985#comment:7>
BuddyPress <http://buddypress.org/>

More information about the buddypress-trac mailing list