BuddyPress Global/Sitewide Unified search update for BuddyPress 1.5+
Jotted by Brajesh Singh in Buddypress, Buddypress Tricks on December 5, 2011It’s a delayed post and I will like to express my sincere apologies to everyone whom I could not reply individually about the Unified Search. Here I am going to put everything you ever asked about unified search in this post.
For those of you who haven’t seen my last post please check the original post here. This post is all about making unifiead search work with BuddyPress 1.5+. It is a long post, so please have a cup of coffee and let us get started.
List of things we will cover below:-
Lets get started now.
Step1:- Installing the BP Global Unified search plugin
Install BP Global Unified search plugin.
Download:-BP Global Unified search
Once you Install and activate this plugin, BuddyPress will give you a notice that you need to create a page and associate it with Search Page.
Here is a screenshot showing the notice
![]()
Please create a WordPress page and associate the page with search page component in BuddyPress->Pages screen.
If you are done with this, let us proceed to the coding part now.
Please note that all the code below should go to your active theme/functions.php until and unless mentioned.
Step2:- Basic setup for theme
Remove the BuddyPress dropwn filters for members/groups/posts etc
//Remove Buddypress search drowpdown for selecting members etc
add_filter('bp_search_form_type_select', 'bpmag_remove_search_dropdown' );
function bpmag_remove_search_dropdown($select_html){
return '';
}
Step 3: Fixing the redirect/page not found for search page
Stop BuddyPress from redirecting to members/other component(by default on search, you are redirected to members directory). Let us stop that behavior.
//force buddypress to not process the search/redirect remove_action( 'bp_init', 'bp_core_action_search_site', 7 );
Step4: Now, let us handle the search page ourself
//let us handle the unified page ourself
add_action( 'init', 'bp_buddydev_search', 10 );// custom handler for the search
function bp_buddydev_search(){
global $bp;
if ( bp_is_current_component(BP_SEARCH_SLUG) )//if thids is search page
bp_core_load_template( apply_filters( 'bp_core_template_search_template', 'search-single' ) );//load the single searh template
}
Step5: Tweaking the Query string for search
We will append the search term to BuddyPress query string.
add_action('advance-search','bpmag_show_search_results',1);//highest priority
/* we just need to filter the query and change search_term=The search text*/
function bpmag_show_search_results(){
//filter the ajaxquerystring
add_filter('bp_ajax_querystring','bpmag_global_search_qs',100,2);
}
//modify the query string with the search term
function bpmag_global_search_qs(){
return 'search_terms='.$_REQUEST['search-terms'];
}
//a utility function
function bpmag_is_advance_search(){
global $bp;
if(bp_is_current_component( BP_SEARCH_SLUG))
return true;
return false;
}
Now we are done with the basic setup. It is time to show the search reults.
Step 6: Create a search-single.php in your theme
In the content area(if you are using default theme, then inside the div “padder”) put the following line
<?php do_action("advance-search");?>
Step7: showing the results
It is time to show the results. Please note, you can change which component’s search is shown first by changing the priority in the add_action below. Basically, we will check for the active component and just add their search result using the add_action, let us start with members.
Members search
//show the search results for member*/
function bpmag_show_member_search(){
?>
<div class="members-search-result search-result">
<h2 class="content-title"><?php _e('Members Results',"bpmag");?></h2>
<?php locate_template( array( 'members/members-loop.php' ), true ) ; ?>
<?php global $members_template;
if($members_template->total_member_count>1):?>
<a href="<?php echo bp_get_root_domain().'/'. bp_get_members_slug().'/?s='.$_REQUEST['search-terms']?>" ><?php _e(sprintf('View all %d matched Members',$members_template->total_member_count),"bpmag");?></a>
<?php endif; ?>
</div>
<?php
}
//Hook Member results to search page
add_action('advance-search','bpmag_show_member_search',10); //the priority defines where in page this result will show up(the order of member search in other searchs)
Similarly, we will add the support for other components.
Adding Group Search
//Group search
function bpmag_show_groups_search(){
?>
<div class="groups-search-result search-result">
<h2 class="content-title"><?php _e('Group Search','bpmag');?></h2>
<?php locate_template( array('groups/groups-loop.php' ), true ) ; ?>
<a href="<?php echo bp_get_root_domain().'/'. bp_get_groups_slug().'/?s='.$_REQUEST['search-terms']?>" ><?php _e("View All matched Groups","bpmag");?></a>
</div>
<?php
//endif;
}
//Hook Groups results to search page
if(bp_is_active( 'groups' ))
add_action('advance-search','bpmag_show_groups_search',15);
Searching Activity Updates
/**activity update search*/
//Activity search
function bpmag_show_activity_search(){
?>
<div class="activity-search-result search-result">
<h2 class="content-title"><?php _e('Activity Updates','bpmag');?></h2>
<?php locate_template( array('activity/activity-loop.php' ), true ) ; ?>
<a href="<?php echo bp_get_root_domain().'/'. bp_get_activity_slug().'/?s='.$_REQUEST['search-terms']?>" ><?php _e("View all matched updates","bpmag");?></a>
</div>
<?php
//endif;
}
//Hook Activity results to search page
if(bp_is_active( 'activity' ))
add_action('advance-search','bpmag_show_activity_search',20);
Showing Blog post search
/**
*
* Show blog posts in search
*/
function bpmag_show_site_blog_search(){
?>
<div class="blog-search-result search-result">
<h2 class="content-title"><?php _e('Blog Search','bpmag');?></h2>
<?php locate_template( array( 'search-loop.php' ), true ) ; ?>
<a href="<?php echo bp_get_root_domain().'/?s='.$_REQUEST['search-terms']?>" ><?php _e("View All matched Posts","bpmag");?></a>
</div>
<?php
}
//Hook Blog Post results to search page
add_action('advance-search',"bpmag_show_site_blog_search",25);
Showing Blogs search(for Multisite)
//show blogs search result
function bpmag_show_blogs_search(){
?>
<div class="blogs-search-result search-result">
<h2 class="content-title"><?php _e('Blogs Search',"bpmag");?></h2>
<?php locate_template( array( 'blogs/blogs-loop.php' ), true ) ; ?>
<a href="<?php echo bp_get_root_domain().'/'. bp_get_blogs_slug().'/?s='.$_REQUEST['search-terms']?>" ><?php _e("View All matched Blogs","bpmag");?></a>
</div>
<?php
}
//Hook Blogs results to search page if blogs comonent is active
if(bp_is_active( 'blogs' ))
add_action('advance-search','bpmag_show_blogs_search',30);
Showing Forum(the Groups forum) topics in the search
//show forums search
function bpmag_show_forums_search(){
?>
<div class="forums-search-result search-result">
<h2 class="content-title"><?php _e("Forums Search","bpmag");?></h2>
<?php locate_template( array( 'forums/forums-loop.php' ), true ) ; ?>
<a href="<?php echo bp_get_root_domain().'/'. bp_get_forums_slug().'/?s='.$_REQUEST['search-terms']?>" ><?php _e("View All matched forum posts","bpmag");?></a>
</div>
<?php
}
//Hook Forums results to search page
if ( bp_is_active( 'forums' ) && bp_forums_is_installed_correctly() && bp_forums_has_directory() )
add_action('advance-search',"bpmag_show_forums_search",35);
Extra bonus for following this tutorial till now:-
Showing Results for BBPress 2.0 plugin
function bpmag_show_bbpress_topic_search(){
$_REQUEST['ts']=$_REQUEST['search-terms'];//put it for bbpress topic search
?>
<div class="bbp-topic-search-result search-result">
<h2 class="content-title"><?php _e('Global Topic Search',"bpmag");?></h2>
<?php bbp_get_template_part('bbpress/content','archive-topic') ; ?>
<?php
global $bbp;
$page = bbp_get_page_by_path( $bbp->root_slug );
?>
<a href="<?php echo get_permalink($page).'?ts='.$_REQUEST['search-terms']?>" ><?php _e("View All matched topics","bpmag");?></a>
</div>
<?php
}
//Hook Blogs results to search page if blogs comonent is active
if(function_exists( 'bbp_has_topics' ))
add_action('advance-search','bpmag_show_bbpress_topic_search',40);
btw, if you find this tutorial a long one, you can simply copy the template/functions.php from bp-global-unified-search and paste it in your theme’s functions.php. Also, I have put a sample search-loop.php in the bp-global-unified-search/template directory. So, you can use it directly or adapt to your theme.
I hope you will like it. Looking forward to hear your suggestions and feedback for further improvement.