Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sc0ttkclark/e5de9d9f2f13964bcecc to your computer and use it in GitHub Desktop.
Save sc0ttkclark/e5de9d9f2f13964bcecc to your computer and use it in GitHub Desktop.
Code to enable having the main bbPress forums on a separate sub-site. Requires: WP Multisite, BuddyPress on main site and network-activated, bbPress on main site and sub-site
<?php
/**
* Remove and add a custom function for bbPress' BuddyPress activity filter
*/
function custom_bbp_notifications_fix() {
if ( !defined( 'BBPRESS_FORUMS_BLOG_ID' ) || !BBPRESS_FORUMS_BLOG_ID ) {
return;
}
remove_filter( 'bp_notifications_get_notifications_for_user', 'bbp_format_buddypress_notifications', 10, 5 );
add_filter( 'bp_notifications_get_notifications_for_user', 'custom_bbp_format_buddypress_notifications', 10, 5 );
add_action( 'bbp_template_before_user_topics_created', 'custom_bbp_switch_to_forums' );
add_action( 'bbp_template_before_user_replies', 'custom_bbp_switch_to_forums' );
add_action( 'bbp_template_before_user_favorites', 'custom_bbp_switch_to_forums' );
add_action( 'bbp_template_before_user_subscriptions', 'custom_bbp_switch_to_forums' );
add_action( 'bbp_template_after_user_topics_created', 'custom_bbp_switch_from_forums' );
add_action( 'bbp_template_after_user_replies', 'custom_bbp_switch_from_forums' );
add_action( 'bbp_template_after_user_favorites', 'custom_bbp_switch_from_forums' );
add_action( 'bbp_template_after_user_subscriptions', 'custom_bbp_switch_from_forums' );
}
add_action( 'init', 'custom_bbp_notifications_fix' );
/**
* Format the BuddyBar/Toolbar notifications
*
* @since bbPress (r5155)
*
* @package bbPress
*
* @param string $action The kind of notification being rendered
* @param int $item_id The primary item id
* @param int $secondary_item_id The secondary item id
* @param int $total_items The total number of messaging-related notifications waiting for the user
* @param string $format 'string' for BuddyBar-compatible notifications; 'array' for WP Toolbar
*/
function custom_bbp_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
if ( 'bbp_new_reply' == $action ) {
custom_bbp_switch_to_forums();
$action = bbp_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format );
custom_bbp_switch_from_forums();
}
return $action;
}
/**
* @var boolean $custom_bbp_switch
*/
global $custom_bbp_switch;
/**
* Switch to forums blog, if it's not the current blog
*/
function custom_bbp_switch_to_forums() {
global $custom_bbp_switch;
$custom_bbp_switch = ( get_current_blog_id() != BBPRESS_FORUMS_BLOG_ID );
if ( $custom_bbp_switch ) {
// Switch to forum site
switch_to_blog( BBPRESS_FORUMS_BLOG_ID );
}
}
/**
* Switch back to current blog, if not the forums blog
*/
function custom_bbp_switch_from_forums() {
global $custom_bbp_switch;
if ( $custom_bbp_switch ) {
restore_current_blog();
}
$custom_bbp_switch = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment