Forum Replies Created
Viewing 1 replies (of 1 total)
-
In reply to: Sub forums in bbpress 2.3 and buddypress 1.7
Hello,
I really needed this feature so I dug into the code and came up with a solution that doesn’t modify core files. The solution hooks the query pre_get_posts and uses the buddypress ‘plugins’ template file to catch and display the group sub forums.
This probably isn’t the most efficient solution but it seems to work in my environment. I’ll post back if I notice any issues as more subforums are created.
Here are the functions – place in your theme’s functions.php. I also included my plugins.php file also.
// functions.php
//Filters bbpress forum permalink if on a group forum page. Keeps users from being redirected to sitewide forums function buddyboss_forum_permalink($link){ global $wpdb, $bp; if (strpos($link, "/forums/forum/")){ $forum = substr($link, strpos($link, "/forum/") + 7); $forums = explode("/",$forum); $args=array( 'post_type' => 'forum', 'name' => $forums[0], ); $root_forum = get_posts( $args ); $group_id = $wpdb->get_var( " SELECT group_id FROM wp_bp_groups_groupmeta WHERE meta_key = 'forum_id' AND meta_value = '". serialize(array($root_forum[0]->ID)) ."'" ); if (isset($group_id)){ $group = groups_get_group( array( 'group_id' => $group_id ) ); unset($forums[0]); $forums = implode("/",$forums); $permalink = bp_get_group_forum_permalink($group)."/".$forums; } else { $permalink = $link; } } return $permalink; } add_filter('bbp_get_forum_permalink','buddyboss_forum_permalink',10,1); //Filters bbpress topic permalink if on a group forum page. Keeps users from being redirected to sitewide forums function buddyboss_topic_permalink($link){ global $wpdb, $bp; if (strpos($link, "/forums/topic/")){ $start_topic = substr($link, strpos($link, "/topic/") + 7); $args=array( 'post_type' => 'topic', 'name' => $start_topic, ); $topic = get_posts( $args ); $topic_forums = get_post_ancestors( $topic[0]->ID ); $root_forum_id = end($topic_forums); $group_id = $wpdb->get_var( " SELECT group_id FROM wp_bp_groups_groupmeta WHERE meta_key = 'forum_id' AND meta_value = '". serialize(array($root_forum_id)) ."'" ); if (isset($group_id)){ $group = groups_get_group( array( 'group_id' => $group_id ) ); array_pop($topic_forums); //Pop group's root forum foreach ($topic_forums as $forum){ $post = get_post($forum); $slug = $post->post_name; $forum_parts .= $slug."/"; } $permalink = bp_get_group_forum_permalink($group)."/".$forum_parts."topic/".$start_topic; } else { $permalink = $link; } } return $permalink; } add_filter('bbp_get_topic_permalink','buddyboss_topic_permalink',10,1); //Filters bbpress reply permalink if on a group forum page. Keeps users from being redirected to sitewide forums function buddyboss_reply_permalink($link){ if (strpos($link, "/forums/reply/")){ $reply = str_replace(site_url()."/forums/reply/","reply/",$link); $forum = str_replace("/forums/forum/".$bp->groups->current_group->slug, "/groups/".$bp->groups->current_group->slug ."/forum", bbp_get_topic_permalink(bbp_get_topic_id())); $permalink = $forum.$reply; } return $link; } add_filter('bbp_get_reply_permalink','buddyboss_reply_permalink',10,1); function buddyboss_bbpress_pre_get_posts($query){ global $bp; if( $query->is_main_query()) { if (bp_current_action() == 'forum'){ if (!empty($bp->action_variables) && !in_array("topic", $bp->action_variables) && !in_array("reply", $bp->action_variables)){ //Sub Forum $forum = end($bp->action_variables); $args = array( 'post_type' => 'forum', 'name' => $forum ); $forums = get_posts( $args ); $parent = $query->get('p'); $query->set('p',''); $query->set('post_parent',$parent); bbpress()->current_forum_id = $forums[0]->ID; bbp_set_query_name( 'bbp_single_forum' ); return $query; } if (empty($bp->action_variables) ) { //Root Group Forum bbp_set_query_name( 'bbp_single_forum' ); return $query; } if (in_array("topic", $bp->action_variables) && !in_array("reply", $bp->action_variables)){ //Topic $topic = end($bp->action_variables); if ($topic == 'edit'){ $edit_key == array_search($topic,$bp->action_variables); $topic = $bp->action_variables[$edit_key-1]; } $args = array( 'post_type' => 'topic', 'name' => $topic ); $topics = get_posts( $args ); $query->set('p',''); $query->set('post_parent',$topics[0]->post_parent); bbpress()->current_forum_id = $topics[0]->post_parent; bbpress()->current_topic_id = $topics[0]->ID; bbp_set_query_name( 'bbp_single_topic' ); if (end($bp->action_variables) == 'edit'){ bbpress()->current_view_id = 'edit'; } return $query; } if (in_array("topic", $bp->action_variables) && in_array("reply", $bp->action_variables)){ //Reply $reply = array_search('reply', $bp->action_variables); $reply_id = $bp->action_variables[$reply+1]; $reply_post = get_post( $reply_id ); $query->set('p',$reply_id); bbpress()->current_topic_id = $reply_post->post_parent; bbpress()->current_reply_id = $reply_id; bbp_set_query_name( 'bbp_single_reply' ); if (end($bp->action_variables) == 'edit'){ bbpress()->current_view_id = 'edit'; } return $query; } } } return $query; } add_action('pre_get_posts','buddyboss_bbpress_pre_get_posts',10, 1); function buddyboss_bbpress_is_edit($retval){ global $bp; if (end($bp->action_variables) == 'edit') $retval = true; return $retval; } add_filter('bbp_is_topic_edit','buddyboss_bbpress_is_edit', 10, 1); add_filter('bbp_is_reply_edit','buddyboss_bbpress_is_edit', 10, 1); function buddyboss_bbpress_form_reply_content($retval){ global $bp; if (end($bp->action_variables) == 'edit' && in_array('topic', $bp->action_variables) && in_array('reply', $bp->action_variables)) { $post = get_post( bbpress()->current_reply_id ); return esc_textarea( $post->post_content ); } } add_filter('bbp_get_form_reply_content','buddyboss_bbpress_form_reply_content', 10, 1); function buddyboss_bbpress_form_topic_content($retval){ global $bp; if (end($bp->action_variables) == 'edit' && in_array('topic', $bp->action_variables) && !in_array('reply', $bp->action_variables)) { $post = get_post( bbpress()->current_topic_id ); return esc_textarea( $post->post_content ); } } add_filter('bbp_get_form_topic_content','buddyboss_bbpress_form_topic_content', 10, 1); function buddyboss_bbpress_form_title(){ global $bp; if (end($bp->action_variables) == 'edit' && in_array('topic', $bp->action_variables) && !in_array('reply', $bp->action_variables)) { $post = get_post( bbpress()->current_topic_id ); return esc_html( $post->post_title ); } } add_filter('bbp_get_form_topic_title','buddyboss_bbpress_form_title', 10, 1); //Filters bbpress admin links if on a group forum page. Keeps users from being redirected to sitewide forums function buddyboss_reply_admin_links( $links, $args ) { global $bp; if (bp_current_action() == 'forum'){ $dom = new DOMDocument; $dom->loadHTML($links); foreach ($dom->getElementsByTagName('a') as $node) { //print_r($node); if ($node->nodeValue == 'Edit'){ $forum = str_replace("/forums/forum/".$bp->groups->current_group->slug, "/groups/".$bp->groups->current_group->slug ."/forum", bbp_get_topic_permalink(bbp_get_topic_id())); $reply = str_replace(site_url()."/forums/reply/","reply/",$node->getAttribute( 'href' )); $node->setAttribute('href', $forum.$reply); } } $links = $dom->saveHTML(); } return $links; } add_filter( 'bbp_get_reply_admin_links', 'buddyboss_reply_admin_links', 10, 2 );
/buddypress/groups/single/plugins.php
<?php global $bp; do_action( 'bp_before_group_plugin_template' ); if (bp_current_action() == 'forum'){ if (empty($bp->action_variables)){ //Forum $bp->groups->current_group->id; $group_forum = groups_get_groupmeta( $bp->groups->current_group->id, $meta_key = 'forum_id'); $group_forum_id = $group_forum[0]; bbpress()->current_forum_id = $group_forum_id; bbp_get_template_part('content','single-forum'); } elseif (!empty($bp->action_variables) && !in_array("topic", $bp->action_variables) && !in_array("reply", $bp->action_variables)){ bbp_get_template_part('content','single-forum'); } elseif (in_array("topic", $bp->action_variables) && !in_array("reply", $bp->action_variables)){ //Topic if (bbpress()->current_view_id === 'edit'){ echo "<div id='bbpress-forums'>"; echo "<a href='".bbp_get_topic_permalink()."'><h4><img src='".get_stylesheet_directory_uri()."/images/back_button.png' />Back to Topic</h4></a>"; bbp_get_template_part('form','topic'); echo "</div>"; } else { bbp_get_template_part('content','single-topic'); } } elseif (in_array("topic", $bp->action_variables) && in_array("reply", $bp->action_variables)){ //Reply if (bbpress()->current_view_id === 'edit'){ echo "<div id='bbpress-forums'>"; echo "<a href='".bbp_get_topic_permalink()."'><h4><img src='".get_stylesheet_directory_uri()."/images/back_button.png' /> Back to Topic</h4></a>"; bbp_get_template_part('form','reply'); echo "</div>"; } else { bbp_get_template_part('content','single-reply'); } } } else { do_action( 'bp_template_content' ); } ?> <?php do_action( 'bp_after_group_plugin_template' );
Viewing 1 replies (of 1 total)