If you have Global Forums by Brajesh Singh installed on your site, you know how versatile that plugin actually is. If you don’t know about it, you really should check it out at BuddyDev.com. It’s a premium plugin that you can download once you’re a member of the site. Membership also gets you access to one of the most helpful Buddypress forums on the net!
Oh wait, it’s a happy day! If you’re using BuddyBuilder on your site, know that all the goodies below are included as options in v1.2. Huzzah! (You’ll find them under the “BP-Extras” tab.)
If you’re not using BuddyBuilder (and I really don’t know why not Image may be NSFW.
Clik here to view. ), this post will show you how you can make Global Forums even more versatile in any Buddypress theme you may be using.
1 -Merge your group forums with Global Forums so all your group topics/posts appear in both.
- Any group created with the forum enabled will automatically get a corresponding forum in the Global Forums directory.
- Any topic or reply posted in one will be automatically mirrored in the other. It works both ways.
- Plus, if groups are set to “Private” or “Hidden”, their forums will not appear in the Global Forums Directory. However, if you change the status of a “Private” or “Hidden” group to “Public”, the forum will automagically appear in Global Forums.
2 – Redirect your users to the corresponding Global Forum anytime they click on a group forum subnav tab.
- The redirect is instant and seamless. Your users won’t even know that there used to be a forum in the group itself (it’s actually still there; they just can’t get to it Image may be NSFW.
Clik here to view.).
- Users are redirected to the correct Global Forum even if you move it to a different parent category or assign it “no parent” at all.
- The forums of groups set to “Private” or “Hidden” will remain accessible only in each group and will not redirect. Change the status to “Public” and they will redirect.
OK. Enough hype… let’s get to it, shall we?
First, let’s merge your group forums with Global Forums and make sure that only forums from groups set to “Public” are displayed in the Global Forums Directory. Copy the code below and paste it in your theme’s functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | add_filter("gf_get_root_forum_id","gf_no_restriction"); add_filter("gf_excluded_forums","gf_get_private_group_forums"); function gf_get_private_group_forums($to_exclude){ global $bp,$wpdb; $query="SELECT bgm.meta_value FROM {$bp->groups->table_name_groupmeta} bgm WHERE bgm.meta_key='forum_id' and bgm.group_id in (select id from {$bp->groups->table_name} bg where bg.status!='public')";//hidden&private $forums=$wpdb->get_col($wpdb->prepare($query)); return $forums; } add_filter("get_forums_where","gf_remove_private_forums"); function gf_remove_private_forums($where){ if(!bp_is_page(GF_SLUG)) return $where; $exclude=gf_get_excluded_forums(); if(!empty($where)&&!empty($exclude)) $where.= " AND "; else if (empty($where)&!empty($exclude)) $where=" WHERE "; if(!empty ($exclude)) $exclude=implode(',', $exclude); $hidden_forums_list=$exclude; if(!empty($hidden_forums_list)) $where.=" forum_id not in (".$hidden_forums_list.")"; return $where; } function gf_no_restriction(){ return 0; } |
Next, let’s add what we need to redirect users to the corresponding Global Forum whenever they click on a group’s “Forum” subnav tab. You’ll notice that the code has a conditional to check the group’s status; if it’s not “Public”, it will not redirect. Copy the code below and paste it directly beneath the code you just pasted in. (If you do not want to redirect your users to Global Forums, simply do not add this bit of code.)
1 2 3 4 5 6 7 8 9 10 11 | add_filter("bp_get_options_nav_forums","my_super_hack_for_group_forum",10,2); function my_super_hack_for_group_forum($forum_link_html, $subnav_item){ global $bp; if( $bp->groups->current_group->status!='public'){ return $forum_link_html;//do not modify the links } else { do_action( 'bbpress_init' );//initialize bbpress $forum_id=groups_get_groupmeta($bp->groups->current_group->id, "forum_id"); return '<li id="' . $subnav_item['css_id'] . '-personal-li" ' . $selected . '><a id="' . $subnav_item['css_id'] . '" href="' . gf_get_forum_permalink($forum_id) . '">' . $subnav_item['name'] . '</a></li>'; } } |
Finally, we’ll wrap everything we just did in a check to make sure this code is only executed if Global Forums is installed and activated. This is important ‘cuz it will prevent your site from breaking if you inadvertently deactivate (or lose) Global Forums. Copy the code below and paste the content of the first line above everything you just pasted. Then paste the content of the second line (the closing bracket) after everything you just pasted.
1 2 | if(function_exists('gf_has_forums')) { } |
Visit your site and prepare yourself to be gleeful… it’s inevitable Image may be NSFW.
Clik here to view.