Hi Andy,
I would recommend adding this code snippet to the skeleton component/group api code/docs too if you’ve not already done so.
Thanks, I’ve now added a codex page for this and linked it in various places:
https://codex.buddypress.org/how-to-guides/checking-buddypress-is-active/
Great, thanks
I know in the past, especially when first starting out, I often used those code snippets as a starting point.
What does this do for backward compatibility of plugins? bp_init is new, right?
The nice thing is by including a separate file you don’t have to do if ( function_exists() / class_exists() / method_exists() ) checks.
@Boone Gorges if you want to be backwards compatible then you could also check for if ( is_defined( ‘BP_VERSION’ ) )
The bp_init action is new in 1.2 and will fire once all of BuddyPress is loaded. It’s called in bp-loader.php if you want to check it out.
A few other actions were added in 1.2 to help plugin authors hook into the correct places within BuddyPress.
bp_setup_globals
bp_setup_nav
bp_setup_root_components
bp_setup_widgets
The intent of those hooks for plugin authors, is to not load your code before BuddyPress does, potentially putting your navigation menus or global component setups out of alignment with BuddyPress. Typical WordPress priority rules apply, so it is still possible to bump your plugin ahead of BuddyPress if you need to.
Hi there!
Does the hook “bp_setup_globals” has been released for plugins in the last 1.2.2 ? I can’t make it work.
Thanks
Those aren’t working currently
These won’t work until 1.3 as the priorities are not set so plugins can access them.
Is there some document/example that works with 1.2? I’ve spend some time with the sample component 1.4 but none of the hooks seems to work.
Regards.
Here’s how I handle it for my BuddyPress Privacy Component. I have a loader file called bp-authz-loader.php. It contains all the necessary plugin metadata at the top of the file. After the metadata, I have this as the first function:
/**
* BPAz_init()
*
* Initialize basic constants and make sure BuddyPress
* is installed and activated. If true, then allow for
* Privacy Component to finish loading.
*
* @since 0.4
*/
function BPAz_init() {
/* Define the component's parent folder name */
define( 'BP_AUTHZ_PLUGIN_NAME', 'bp-authz' );
/* Define component's directory and URL Paths */
define( 'BP_AUTHZ_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . BP_AUTHZ_PLUGIN_NAME );
define( 'BP_AUTHZ_PLUGIN_URL', WP_PLUGIN_URL . '/' . BP_AUTHZ_PLUGIN_NAME );
/* Define the directory where user settings' functions reside */
define( 'BP_AUTHZ_SETTINGS', BP_AUTHZ_PLUGIN_DIR . '/' . 'settings' );
/* BuddyPress is installed and activated, finish initialization and go! */
require_once( BP_AUTHZ_PLUGIN_DIR . '/bp-authz-core.php' );
}
add_action( 'bp_init', 'BPAz_init' );
I have two more functions in this file, one to register the plugin upon activation, the other to register it upon deactivation. But that is all. Just these three functions.
If BuddyPress is installed and activated, BPAz_init() will fire, resulting in the rest of my component being activated when bp-authz-core.php is loaded. I’ve tested it and it works as expected without causing BP to crash. In other words, if BP is not activated but my plugin is activated, nothing happens. This is what you want.