Changing hooked-to action functions to make new BP hooks work for 3rd-party components
|Reported by:||jeffsayre||Owned by:||johnjamesjacoby|
This is an important fix with a long explanation.
Currently, there are five new action hooks in BuddyPress 1.2.x that are intended to be used by 3rd-party components but in reality they do not work. Here's why.
NOTE: Although this explanation focuses on the bp_setup_nav action event (hook), the following logic also applies to these BuddyPress action hooks as well: bp_setup_globals, bp_setup_root_components_action, bp_register_widgets, bp_register_activity_actions.
Suggested patches to all of these hooks are provided in the attached file(s).
Here’s the array of all action functions associated with the plugins_loaded action event. The element keys represent the priority set for each added action function. If a given action function does not have a priority set, it automatically is set to a default of “10”.
[plugins_loaded] => Array (
 => Array ( [wp_maybe_load_widgets] => Array ( [function] => wp_maybe_load_widgets [accepted_args] => 1 ) [wp_maybe_load_embeds] => Array ( [function] => wp_maybe_load_embeds [accepted_args] => 1 ) )
 => Array ( [bp_core_setup_root_uris] => Array ( [function] => bp_core_setup_root_uris [accepted_args] => 1 ) [bp_setup_root_components] => Array ( [function] => bp_setup_root_components [accepted_args] => 1 ) )
 => Array ( [bp_core_set_uri_globals] => Array ( [function] => bp_core_set_uri_globals [accepted_args] => 1 ) )
 => Array ( [bp_core_load_buddypress_textdomain] => Array ( [function] => bp_core_load_buddypress_textdomain [accepted_args] => 1 ) [bp_setup_globals] => Array ( [function] => bp_setup_globals [accepted_args] => 1 ) )
 => Array ( [bp_setup_nav] => Array ( [function] => bp_setup_nav [accepted_args] => 1 ) [bp_setup_widgets] => Array ( [function] => bp_setup_widgets [accepted_args] => 1 ) [bp_register_activity_actions] => Array ( [function] => bp_register_activity_actions [accepted_args] => 1 ) [bp_loaded] => Array ( [function] => bp_loaded [accepted_args] => 1 ) ) ) [shutdown] => Array (  => Array ( [wp_ob_end_flush_all] => Array ( [function] => wp_ob_end_flush_all [accepted_args] => 1 ) )
When the do_action function is processing a call from a given action event [ in this case do_action( ‘plugins_loaded’, ‘...’ ) ], it will sequentially loop through this array, firing the first action function in the first array element then proceeding to the next. A given array element can have more than one added action function. This happens when the same priority is set in the add_action call. In fact, you can see that the third array element is the only one that does not have more than one action function.
The reason that the bp_setup_nav hook does not work for any BP-dependent plugins can be discovered by looking at the fifth element in the plugins_loaded array (the one with a key of “10”).
All BuddyPress plugins now have a means of activating only if and when BuddyPress is active by hooking into the bp_init action event. The function that contains that hook is bp_loaded found on line 65 of the trunk version bp-loader.php. This function is hooked to the plugins_loaded event.
Once again, look at the fifth element in the plugins_loaded array. You will find the reference to the added action function bp_loaded. Now, look at where the reference to the added action function bp_setup_nav is located. It is the very first subarray element in the fifth element in the plugins_loaded array.
What does this mean? It means that the bp_setup_nav action function will fire before the bp_loaded action function fires. Since the bp_setup_nav action function contains the bp_setup_nav hook, it means that that hook will be finished firing before the bp_init hook is fired. Which means it gets fired before any BP-dependent plugins have been initialized.
In this case, it is useless for BP-dependent plugins to attempt to hook into the bp_setup_nav action event. By the time they are active, it is already too late.
- Change the priority to 11 or higher
- Hook the bp_setup_nav function to the bp_init action event instead
- Create a separate, new action event exclusively for hooking these five action functions
Although it is possible to fix each of the five BuddyPress action hooks by simply adding a priority lower than 10 to the plugins_loaded action event, I suggest hooking these action functions to the bp_init action event instead. It makes more sense to base the triggering of theses five action events on the bp-load action event. This is what has been done in the patch file(s). However, even with that change, the priority for each added action function will still need to be set to lower than 10 to allow for all dependent plugins to load before the action events are triggered.
Change History (27)
- Milestone changed from 1.2.4 to 1.3
- Priority changed from critical to major
- Resolution set to fixed
- Status changed from assigned to closed
- Resolution fixed deleted
- Status changed from closed to reopened
- Resolution set to fixed
- Status changed from reopened to closed