BP Profile Search
[Updated February 14, 2011]
When I had a chance to look into BuddyPress, the ‘social networking in a box’ for WordPress, I noticed that a plugin to search, or filter, the members directory was still missing.
So I released BP Profile Search. BP Profile Search adds a configurable search form to the Members directory page, so visitors can find site members searching their extended profiles.
This page contains the updated documentation for BP Profile Search 2.4.
Features
BP Profile Search shows your visitors a form to search or filter your Members directory. In the plugin admin page you can access the following options:
* Specify the text for the form header and welcome message (HTML enabled).
* Enable the show/hide form feature.
* Select the profile fields to include in the search form (currently the datebox profile fields are not supported).
* If your extended profiles include a birth date field, enable the age range search, so your visitors can specify the minimum and maximum age for their search.
* Select the search mode for text fields, between:
– partial match, where a search for John matches field values of John, Johnson, Long John Silver, and so on;
– exact match, where a search for John matches the field value John only.
In both modes the wildcard characters % (percent sign), matching zero or more characters, and _ (underscore), matching exactly one character, are available to your visitors to better specify their search.
* Select the members list to filter, if your Members Directory page contains more than one.
Installation
After the standard manual or automatic plugin installation procedure, you’ll be able to access the plugin admin page BuddyPress -> Profile Search.
Before you can use the plugin, you have to add the profile search form to your BuddyPress Members page.
If you are using the BuddyPress default theme, open index.php in the buddypress/bp-themes/bp-default/members folder, locate this line (around line 14):
</div><!-- #members-dir-search -->
and, right after that, insert the BP Profile Search form:
<?php do_action ('bp_profile_search_form'); ?>
If you are not using the BuddyPress default theme, you have to insert the BP Profile Search form somewhere in your theme Members page.
Troubleshooting
- If your search always returns the full members list, try changing the Filtered Members List value in the Advanced Options tab.
Conclusions
I hope you find this plugin useful and help me improve it, with your suggestions, bug reports, and patches. BP Profile Search is available for download in the WordPress Plugin Directory and in the BuddyPress Plugin Directory.
Anyone have custom css for this filtering? With a lot of options the page gets very long, when most can be placed along-side each other!
Hi Jason,
The template for the search form is in the plugin file bps.searchform.php, and you can change it there. I can’t offer more help because I’m not an HTML or CSS expert, but I hope I’ll be able to write a simpler ‘template system’, sooner or later!
Andrea,
Is there a way to limit checkbox options in the search form to those that will return at least one member? That way people using the search will not get “none found” when choosing checkbox options.
Thank you so much.
Glenn
Hi Glenn,
Unfortunately that’s not possible yet, but it’s a very good suggestion and I’ll add it to my to-do list.
@andrea
I got this to work!
I copied the bp_get_the_profile_field_options() function that is in bp_xprofiles_templates.php into a new function that I called bp_get_the_profile_field_options_only_used(). A few lines from the top of that function, $objects = $field->get_children();. The $objects variable is what determines what is displayed on the searchform. It is an array of objects containing the name of the checklist and other profile fields.
I created an array of all of the names of the checkboxes that members have checked for their profiles.
$admins = get_users(‘role=administrator&fields=ID’);
$twamembers = get_users(‘role=s2member_level1&fields=ID’);
$members = array_merge($admins, $twamembers); //gets all member ids that are admins or twa members
foreach($members as &$member){
//get data from checkbox fields for each member redefining $members as the data in a multidimensional array
$member = bp_unserialize_profile_field( xprofile_get_field_data(bp_get_the_profile_field_name(), $member));
}
//turns multidimensional array into a single-dimensional array of all the data
$output = array();
// Push all $val onto $output.
array_walk_recursive($members, create_function(‘$val, $key, $obj’, ‘array_push($obj, $val);’), &$output);
$members = array_unique($output); //get rid of duplicates
Then I modified $objects to delete any objects whose name is not in the array above.
foreach($options as $key => $option1){
if(!in_array($option1->name, $members, false)){
unset($options[$key]);
}
}
$options = array_values($options);
Now I just called the modified function in bps_searchform.php.
This seems to be working for me so far. This produces a shortened checklist that will always returns at least 1 member in the search.
Hopefully this shortens your to-do list.
Thanks for the great plug-in,
Glenn
@Glenn
Oops,
In my previous explanation, I talked about changing the $objects variable when it is actually the $options variable.
Hi Glenn,
I’ll add your patch to BP Profile Search in the next release. Thank you very much!
@Jason
Jason,
I just got this working. The css is pretty easy, but the template modification was a little tricky.
The problem is that all of the checkbox items have label tags instead of li tags. So you have to go into the template and add these tags. The only problem is that the checkbox items are all called by a function that also renders the html.
So you have to go to the function bp_get_the_profile_field_options() located in plugins/buddypress/xprofile/xprofile_template.php and modify it by adding the following code:
$html .= ”;
$html .= apply_filters( ‘bp_get_the_profile_field_options_checkbox’, ‘id . ‘[]” id=”field_’ . $options[$k]->id . ‘_’ . $k . ‘” value=”‘ . esc_attr( stripslashes( $options[$k]->name ) ) . ‘”> ‘ . esc_attr( stripslashes( $options[$k]->name ) ) . ”, $options[$k], $field->id, $selected, $k);
$html .= ”;
This goes at the very end of the “case ‘checkbox’:” section (line 562 on my editor).
You’ll note that I added li tags in two places. For some reason, the li tags in the block of code did not show up in the search form but did in other places. It probably has something to do with that filter.
I would recommend moving this function to your functions.php in your child theme so your work is not destroyed next time buddypress is updated.
Then go to the plugins/bp_profile_search/bps_searchform.php and add ul tag around the function above. It is on line 111 in my editor. Here is the code:
The name of the function is different in the example above because I changed the name of that function for another issue. Yours will still be called bp_the_profile_field_options().
The above will list the checkboxes as an unordered list with the class “checkbox.”
I assume you would do the exact same thing for radio buttons. Those codes are in the same files.
Now insert the following into your style.css:
.label{
font-size:14px;
font-weight: bold;
display: block;
}
div.checkbox{
position:relative;
display:block;
width: 100%;
height: auto;
}
.clear { clear: both; }
ul.checkbox{
margin: 0 auto;
padding: 0;
}
ul.checkbox li{
text-align: left;
float: left;
list-style: none;
height: 30px;
width: 195px;
}
div.field-wrapper ul {
list-style-type: none;
}
You may have to play with it a little to fit with your theme, but it should be close.
If you want to create columns for checklists in other places where there are checkboxes for profile fields, you will need to go into those templates and just add ul class=”checkbox” around that same function.
Andrea, if you want to add the above change to the new function you will have to create for this patch
then it will allow people to style the checkboxes more easily.
Good luck!
Glenn
Thank you Glenn.
Unfortunately WordPress deletes HTML in comments, but if you send me the missing lines by email I’ll be glad to add them back to your comment.
I have installed the plugin and added the the php code to the members index.php as stated. When I view the page I don’t see any search options. What am I missing?
Probably you need to configure the plugin and enable the fields for the search form. They start off as disabled, I think I’ll change that in a future release.
@andrea Hi Andrea, I have made all of the necessary changes to the settings, selected the search fields but it still doesn’t appear.
Here is my index.php in the directory listed in the instructions.
<a href="”><?php printf( __( 'All Members %s’, ‘buddypress’ ), bp_get_total_member_count() ); ?>
<a href="”><?php printf( __( 'My Friends %s’, ‘buddypress’ ), bp_get_total_friend_count( bp_loggedin_user_id() ) ); ?>
@Jason
Hello Jason,
I am looking to do the exact same thing here, did you figure out how to do this? If so, what changes were made?
Thanks in advance.
Hello,
First off all, thank you for this plugin. I found exactly what I want to do with it.
I found an issue with wordpress 3.2.1 and buddypress 1.5.1.
By default in buddypress, the age extented profile field is a datebox which store a date in mysql date format (YYYY-MM-DD HH-MM-SS) in mysql database.
The problem is that the search by range date given by BP profile search do a querie with timestamp…
I have made some change wich can be include in the next release if someone else have the same issue :
case ‘datebox’:
if ($id != $bps_options['agerange']) continue;
$time = time ();
$day = date (“j”, $time);
$month = date (“n”, $time);
$year = date (“Y”, $time);
$from1 = $year – $value;
$to1 = $year – $to -1;
$min = “$to1-$month-$day”;
$max = “$from1-$month-$day”;
$sql = “SELECT user_id from {$bp->profile->table_name_data}”;
$sql .= ” WHERE field_id = $id AND value >= ‘$min’ AND value <= '$max'";
break;
}
Thanks
Hi Julien,
That’s great! Thank you very much for this patch, I’ll add it to the plugin as soon as possible.
@Julien
I tried to implement your changes and got:
Parse error: syntax error, unexpected T_STRING in /wp-content/plugins/bp-profile-search/bps-functions.php on line 139
The Line 139 it is complaining about is “$from1 = $year – $value;”
Hi Joe,
Please make sure you don’t copy and paste the code, you’ll need to retype it, or at least retype the quote characters (both single and double quotes).
@andrea
Thanks. Managed to change all the ‘ ” and –
It works now!
Hi guys,
Having a big issue with this plugin now that required field behaviour in BP has changed.
No idea why BP changed this behaviour but when you set a field to required that offers a drop down to select from, the first option in the dropdown is selected as default.
The fields no longer has a ——— option at the top, which would ommit it from a search, it MAKES YOU select an answer from the dropdown.
This means that the first option is ALWAYS selected on the search form, which means if you have multiple required fields, a search is impossible as the first option in ALL fields is selected by default.
This way you can’t search by just one field, as all have to be selected.
I know this sounds confusing, but if you set up some profile field dropdowns and make them required you’ll see instantly what I mean.
Such a shame, as BP’s behaviour makes this plugin unusable when you have required fields.
Anything that could be done?
Thanks so much for your time!
Hi Ross,
Thank you for your feedback, I didn’t realize that BP 1.5 changed the required field behaviour.
I’ll look into this over the coming holidays. Should you solve this issue before I do, I’ll happily accept a patch!