Making Global Unified search for BuddyPress to work with WordPress adminbar search form
Many of the members asked me about integrating global unified search to WordPress Adminbar search form. Last night, I played with the code and managed to make it work.
In this post, I am going to outline the way I have done it on my setup. It's simple and just a couple of lines of code.
Since we don't want to modify the existing code for global search, all we need is redirect the search result page to our unified search result page.
The simplest way is to change the action of the adminbar search form. WordPress does not provide a hook to filter the search form action url, so we will simply remove the default sarch form and include our own search form.
Step 1: Remove the default search form
[sourcecode language="php"]
function remove_wp_adminbar_search_menu(){
//remove the search from from adminbar menu
remove_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
}
add_action('init','remove_wp_adminbar_search_menu');
[/sourcecode]
Now that we have removed the search form from admin bar menu, we will need to add our own search form. The code below is simply a copy of wp_admin_bar_search_menu, with the form action url modified.
Step 2: Add our own modified search form
[sourcecode language="php"]
function custom_admin_bar_search_menu( $wp_admin_bar ) {
if ( is_admin() )
return;
//we have just changed the form url
$form = '<form action="' . esc_url( bp_search_form_action() ) . '" method="get" id="adminbarsearch">';
$form .= '<input class="adminbar-input" name="s" id="adminbar-search" tabindex="10" type="text" value="" maxlength="150" />';
$form .= '<input type="submit" class="adminbar-button" value="' . __('Search') . '"/>';
$form .= '</form>';
$wp_admin_bar->add_menu( array(
'parent' => 'top-secondary',
'id' => 'search',
'title' => $form,
'meta' => array(
'class' => 'admin-bar-search',
'tabindex' => -1,
)
) );
}
add_action( 'admin_bar_menu', 'custom_admin_bar_search_menu', 4 );
[/sourcecode]
If the above code does not work, please use this gist
That's it.
You will need to put this code into functions.php of your theme and it will work.
Please do let me know if you use it and whether it works for you or not.
Wow Brajesh! Will certainly check this out. Thank you 🙂
Hi Mercime,
Thank you 🙂
I am looking forward to it 🙂
Many thanks, Brajesh.
However on a local install i am getting the following errors
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ' remove_wp_adminbar_search_menu' not found or invalid function name in /Users/me/Documents/Websites/mydomain.dev/wp-includes/plugin.php on line 403
Warning: Cannot modify header information – headers already sent by (output started at /Users/me/Documents/Websites/mydomain.dev/wp-includes/plugin.php:403) in /Users/me/Documents/Websites/mydomain.dev/wp-content/plugins/bp-group-reviews/bp-group-reviews.php on line 122
When I disable bp-group-reviews the first error remains.
It must be my set-up.
Would love it to work.
Hi Val,
There was a space in the
[sourcecode language="language='php"]
add_action('init','remove_wp_adminbar_search_menu');
[/sourcecode]
I have just remove that. Please copy the code again and it will work now.
Regards
Brajesh
Amazing we're on at the same time
But Parse error: syntax error, unexpected T_STRING in
remove_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
Many thanks,
Val
Hi Val,
Please make sure you have not copied the line numbers. Can you post your code on pastebin ?
Hi Brajesh,
see http://pastebin.com/7d7jFdbG
thank you
Hi Val,
I am sorry.
I just checked the code you copied had many invisible characters. The problem is with the syntax hi-lighter plugin I am using.
For now, I have embedded a gist at the bottom of the post.
That should solve your problem.
Please do let me know if that works or not?
Hi Brajesh,
Yes it is now functional from the admin bar. Thank you.
But….. (am i a pest)
The search appears to me (at first glance, and a very tired glance, though you must be approaching dawn) to be scooping up absolutely everything and not actually searching.
I am comparing it to your global unified search which I have in a sidebar widget and is in fact very accurate.
So i have a member — Bero.
Your original search returned a single result in members Bero.
Your admin bar search returned every member, every activity, every blog.
there were no groups found —-> correct
But I believe when you have this tweaked it will really be a tremendous addition.
Hi Brajesh,
I am wondering whether its my set up ? The search mechanism is installed in the admin bar.
it does return segmented results. But the results include everything. Whereas the search from the previous incarnation appears to return accurate results from each segment.
the urls returned are
my domain.dev/search/?s=bero admin bar search
my domain.dev/search/ previous global unified search
I'm having the same problem.
Do you have an idea of how to do search for an individual user's activity.
@all,
sorry I could not reply in past. If you are facing any issue with this plugin, please let me know, I promise to help quickly in future 🙂
I have the same problem as Valuser and Skyrie, my admin bar search returns everything as a search result. Searching from the header works fine.
Thank you for the comment.
I am going to revisit it today. There may be some problem.
Will reply again after testing.
Brajesh! You are the BEST!!!!
Thank you for the appreciation Stefan 🙂
Hi Brajesh!,
Has there been an update on this – the admin bar use of your plugin ?
In any event, many thanks.
Hi Val,
Yes, you were right about the issue. I was too late to understand it. I have fixed it. Will push over to github and inform you here in an hour.
Hi Val,
I have committed the updated code to github.
Here is a small change I have done in functions.php
https://github.com/sbrajesh/bp-global-unified-search/commit/a42753dbabd4012f1ae35f8d857fb3107b89c468
hope that helps.
Regards
Brajesh
Hi Brajesh,
Thanks for your all incredible plugins. I am stuck at finding file containing the filter items of search form. I want to remove posts from the top search filters. Can you please point to the correct file. I also want to remove forums link from the top right corner user profile drop down menu. Thanks.
Hi Kunal,
I am sorry I could not reply earlier.
I am unable to understand your issue, cane you please clarify a little bit ?
Hi Brajesh,
Thanks for the reply, I am a student and am maintaining an IT quiz site where I don't need any forum post or blog option. All I need is activity where people can come and post their questions. If you can see, in the top right user hover menu bar, there is a list of sub-menu items viz. username, Edit my profile, Log out, Activity, Forums, Friends, Groups, Messages and so on.
I want to remove Forums option from there.
Also in the drop down search menu in the header, there are members, posts, groups and activity option(activity came after using your bp searchable activity plugin). I want to remove posts.
In the region below, the header we have "show" with a drop down menu containing all the above mentioned links. You can check the site http://www.tcstechbytes.com. Thanks for your time.
And I noticed one very peculiar thing when searching for an activity. The last searched activity term remains in the search box and I have made my activity as the front page so when I click on the "Home" button, I keep on getting the same activity search result.
Then all I need to do get out of it is, to manually clear the search box and again search the activity with empty search box. The "Home" button works normally henceforth and loads all the activity instead of the previous searched activity.
Can there be something like clearing the search box automatically after the search is made ? Thanks for your hard work and concern.
And please don't get me wrong for posting so much. I'm using quite a few plugins of yours and am really thankful for it. I really like the chat plugin which is very similar to facebook. But It would be killing plugin for chat if you could add support for emoticons and somewhat increase the speed as well. It takes some 1 second to send the message. Thanks again.