Often, it doesn’t matter whether your Buddypress plugin loads before or after Buddypress. However, if you are using groups API, or extending other Buddypress plugins, your class extensions require that Buddypress be loaded beforehand. Functions hooked into actions that occur before Buddypress loads that use Buddypress functions will cause problems as well if the load order is wrong.

WordPress loads plugins in alphabetical order. So the simplest (and most janky) solution is to name your plugin something that comes after “buddypress” in the alphabet. But there are better methods that you should use instead.

For versions before BP 1.2

The goal here is to check whether Buddypress has been loaded at the point when your plugin loads, and if it has not, load it manually. The manual load will prevent the normal load from happening later, so you won’t get double what you need.

  1. //hack required to load BP first
  2. function bpgc_load_buddypress() {
  3.         //buddypress is loaded
  4.         if ( function_exists( ‘bp_core_setup_globals’ ) )
  5.                 return false;
  6.  
  7.         // Get the list of active sitewide plugins
  8.         $active_sitewide_plugins = maybe_unserialize( get_site_option( ‘active_sitewide_plugins’ ) );
  9.         $bp_activated = $active_sitewide_plugins[‘buddypress/bp-loader.php’];
  10.  
  11.         //bp is not activated
  12.         if ( !$bp_activated ){
  13.                 return false;
  14.         }
  15.  
  16.         //bp is activated but not yet loaded
  17.         if ( $bp_activated ) {
  18.                 return true;
  19.         }
  20.  
  21.         return false;
  22. }
  23.  
  24. //load bp if its not activated
  25. if ( bpgc_load_buddypress() ){
  26.         require_once( WP_PLUGIN_DIR . ‘/buddypress/bp-loader.php’ );
  27. }

If you are using BP 1.2+

Fortunately, they made this much easier to do in BP 1.2. For my plugin, all I needed to do was:

  1. function bpgc_bp_loaded(){
  2.         require ( WP_PLUGIN_DIR . "/BP-Group-Control/bpgc-classes.php");
  3. }
  4. add_action(‘bp_init’, ‘bpgc_bp_loaded’);

… in order to load my classes only once Buddypress has finished loading. Probably the safest way to ensure that your plugin completely loads after Buddypress is to create a loader.php file that contains this action and loads all of your other files.

Leave a Reply